Skip to content

Commit

Permalink
assign a new hash to url when doing ajax submit form
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxi committed Aug 15, 2014
1 parent 00fe0eb commit d014aab
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports = function(grunt) {
'lib/base.js',
'lib/util.js',
'lib/view.js',
'lib/route.js'
'lib/route.js',
'vendor/sha1.js'
],
dest: 'dist/<%= pkg.name %>'
}
Expand Down
2 changes: 2 additions & 0 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ APP.facadeURI_ = function(url) {
};

function route_() {
if (/^!(\d|\w)+/.test(window.location.hash)) return;

var params;
try {
params = APP.routeParams_(window.location.hash);
Expand Down
10 changes: 10 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/*global jsSHA */

var _ = {};

_.has = function(a, b) {
Expand All @@ -16,3 +18,11 @@ _.isEmpty = function(a) {
return !a;
}
};

_.getHash = function(a) {
if (typeof a === 'object') {
a = JSON.stringify(a);
}
var str = 'blob ' + a.length + '\0' + a;
return (new jsSHA(str, 'TEXT')).getHash('SHA-1', 'HEX');
};
3 changes: 3 additions & 0 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ function interceptFormSubmit_(event, form) {
var container = APP.container_;
event.stopImmediatePropagation();
event.preventDefault();
var formSubmitParams = null;
var xhr = jQuery(form).ajaxSubmit({
url: form.action,
beforeSubmit: function(formData, context, options) {
APP.componentWillSubmitForm(formData, form, options);
formSubmitParams = jQuery.extend({}, formData);
}
}).data('jqxhr');
xhr.done(function(resp) {
container.empty();
APP.componentWillUpdate_(container, resp);
container.html(resp);
APP.componentDidUpdate_(container, resp);
APP.goURI('!' + _.getHash(formSubmitParams));
});
return false;
}
Expand Down
21 changes: 21 additions & 0 deletions vendor/sha1.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d014aab

Please sign in to comment.