-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone-pjax.js
71 lines (67 loc) · 2.2 KB
/
backbone-pjax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Backbone.Pjax = (function () {
"use strict";
var History = Backbone.History.extend({
"navigate": function(fragment, options) {
if (!Backbone.History.started) return false;
if (!options || options === true) options = {trigger: options};
fragment = this.getFragment(fragment || '');
if (this.fragment === fragment) return;
this.fragment = fragment;
var url = this.root + fragment;
if (!this._hasPushState) {
if (this._wantsHashChange) {
this._updateHash(this.location, fragment, options.replace);
if (this.iframe && (fragment !== this.getFragment(this.getHash(this.iframe)))) {
if(!options.replace) this.iframe.document.open().close();
this._updateHash(this.iframe.location, fragment, options.replace);
}
} else {
return this.location.assign(url);
}
}
if (options.trigger) this.loadUrl(fragment);
}
});
var Router = Backbone.Router.extend({
"pjax" : {
},
"getPjaxContainer" : function () {
return this.pjax.container;
},
"getPjaxOptions" : function () {
return this.pjax.options;
},
"pjaxInit" : function () {
$(document).pjax('a', this.getPjaxContainer(), this.getPjaxOptions());
this.registerPjaxEvents();
this.on('pjax:complete', this.pjaxNavigate);
},
"pjaxNavigate" : function () {
this.navigate(location.pathname.substr(1), true);
},
"registerPjaxEvents" : function () {
var router = this;
var $document = $(document);
_.each(['start', 'end'], function(name) {
$document.on('pjax:'+name, function(e, options) {
router.trigger('pjax:'+name, e, options);
});
});
_.each(['beforeSend', 'send', 'complete', 'success', 'error', 'timeout'], function (name) {
$document.on('pjax:'+name, function(e, args) {
router.trigger('pjax:'+name, e, args);
});
});
return router;
},
"navigate" : function (fragment, options) {
Backbone.history.navigate(fragment, options);
return this;
}
});
Backbone.history = new History();
return {
"History" : History,
"Router" : Router
};
})();