Skip to content

Commit

Permalink
First version of historyState argument to Location.go.
Browse files Browse the repository at this point in the history
For #4
  • Loading branch information
tmeasday committed Sep 17, 2014
1 parent c78832a commit 6105b2e
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 28 deletions.
6 changes: 6 additions & 0 deletions examples/historyState/.meteor/.finished-upgraders
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file contains information which helps Meteor properly upgrade your
# app when you run 'meteor update'. You should check it into version control
# with your project.

notices-for-0.9.0
notices-for-0.9.1
1 change: 1 addition & 0 deletions examples/historyState/.meteor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local
7 changes: 7 additions & 0 deletions examples/historyState/.meteor/.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file contains a token that is unique to your project.
# Check it into your repository along with the rest of this directory.
# It can be used for purposes such as:
# - ensuring you don't accidentally deploy one app on top of another
# - providing package authors with aggregated statistics

r44vi31sejv9otpmybw
1 change: 1 addition & 0 deletions examples/historyState/.meteor/cordova-plugins
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

10 changes: 10 additions & 0 deletions examples/historyState/.meteor/packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Meteor packages used by this project, one per line.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.

meteor-platform
autopublish
insecure
iron:location

1 change: 1 addition & 0 deletions examples/historyState/.meteor/release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]
25 changes: 25 additions & 0 deletions examples/historyState/historyState.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<head>
<title>historyState</title>
</head>

<body>
<h1>Welcome to Meteor!</h1>

{{> hello}}
</body>

<template name="hello">
<h1>Current state:</h1>
<pre>{{historyState}}</pre>

<form id="push">
<label>Push State</label>
<input type="text" name="push">
<button type="submit">Push</button>
</form>
<form id="replace">
<label>Replace State</label>
<input type="text" name="replace">
<button type="submit">Replace</button>
</form>
</template>
19 changes: 19 additions & 0 deletions examples/historyState/historyState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if (Meteor.isClient) {
Template.hello.helpers({
historyState: function() {
return Iron.Location.get().options &&
Iron.Location.get().options.historyState;
}
});

Template.hello.events({
'submit #push': function(e, t) {
e.preventDefault();
Iron.Location.go('/' + Random.id(), {historyState: t.$('input').val()});
},
'submit #replace': function(e, t) {
e.preventDefault();
console.log("Can't do this yet");
}
})
}
45 changes: 20 additions & 25 deletions lib/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ var set = function (state) {

var setStateFromEventHandler = function () {
var href = location.href;

var state;

if (isUsingHashPaths()) {
var state = new State(urlFromHashStyle(href));
set(state);
state = new State(urlFromHashStyle(href));
} else {
var state = new State(href);
set(state);
state = new State(href, {historyState: history.state});
}

set(state);
};

var fireOnClick = function (e) {
Expand All @@ -61,27 +62,21 @@ var fireOnClick = function (e) {
/**
* Go to a url.
*/
var go = function (url) {
if (isUsingHashPaths()) {
var state = new State(url);

if (set(state)) {
var go = function (url, options) {
var state = new State(url, options);

if (set(state)) {
Deps.afterFlush(function () {
// if after we've flushed if nobody has cancelled the state then change
// the url.
Deps.afterFlush(function () {
if (!state.isCancelled())
if (!state.isCancelled()) {
if (isUsingHashPaths()) {
location.hash = fixHashPath(url);
});
}
} else {
var state = new State(url);

if (set(state)) {
Deps.afterFlush(function () {
if (!state.isCancelled())
history.pushState(null, null, url);
});
}
} else {
history.pushState(state.options.historyState, null, url);
}
}
});
}
};

Expand Down Expand Up @@ -229,8 +224,8 @@ Location.onClick = function (fn) {
/**
* Go to a new url.
*/
Location.go = function (url) {
return go(url);
Location.go = function (url, options) {
return go(url, options);
};

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/state.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var Url = Iron.Url;

State = function (url) {
_.extend(this, Url.parse(url));
State = function (url, options) {
_.extend(this, Url.parse(url), {options: options || {}});
};

// XXX: should this compare options (e.g. history.state?)
State.prototype.equals = function (other) {
if (!other)
return false;
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
]
],
"pluginDependencies": [],
"toolVersion": "[email protected].30",
"toolVersion": "[email protected].29",
"format": "1.0"
}

0 comments on commit 6105b2e

Please sign in to comment.