-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.router.flow.js
140 lines (139 loc) · 4.73 KB
/
backbone.router.flow.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Generated by CoffeeScript 1.6.3
(function() {
(function(window, document, jQuery, Backbone) {
var $, Deferred;
$ = jQuery;
Deferred = $.Deferred;
return Backbone.Router = Backbone.Router.extend({
defs: [],
visited: [],
pushDefer: function(defer) {
if (defer && defer.done) {
this.defs.push(defer);
return defer;
} else {
return new Deferred().resolve();
}
},
debug: true,
log: function() {
if (this.debug) {
return console.log.apply(console, arguments);
}
},
isFirstTime: function() {
return this.prevDefer === void 0;
},
getCurrentUrl: function(l) {
if (l == null) {
l = location;
}
return l.pathname.replace(Backbone.history.root, "/") + l.search + l.hash;
},
_bindRoutes: function() {
var routes,
_this = this;
if (!this.routes) {
return;
}
this.routes = _.result(this, 'routes');
routes = _.keys(this.routes);
return _.each(routes, function(i) {
var callback, name;
name = _this.routes[i];
if (!_.isString(name && !_.isFunction(name))) {
callback = function(fragments) {
return _this.activate.apply(_this, [name, fragments]);
};
}
return _this.route(i, name, callback);
});
},
navigate: function(fragment, interrupt, options) {
if (options == null) {
options = true;
}
if (interrupt === true) {
this.interrupt();
}
Backbone.history.navigate(fragment, options);
return this;
},
activate: function(c, args) {
var defer, hasPrev, url, _args,
_this = this;
if (args == null) {
args = null;
}
this.log("\n=================", this.url = url = this.getCurrentUrl());
this.prevDefer = this.pushDefer(this.isFirstTime() ? typeof this.firstTime === "function" ? this.firstTime(c, url) : void 0 : this.prevDefer);
_args = {
args: args,
url: url,
prevView: this.prevObj,
prevUrl: this.prevUrl,
router: this,
view: c
};
hasPrev = this.prevUrl || this.prevObj;
defer = this.prevDefer.then(function() {
if (!hasPrev) {
return;
}
_this.log(" LEAVING", _this.prevUrl);
return _this.pushDefer(typeof _this.beforeEachLeave === "function" ? _this.beforeEachLeave.apply(_this, [_args]) : void 0);
}).then(function() {
var d, _ref;
if (!hasPrev) {
return;
}
d = _this.pushDefer((_ref = _this.prevObj) != null ? typeof _ref.leave === "function" ? _ref.leave.apply(_ref, [_args]) : void 0 : void 0);
return d.done(function() {
return _this.log(" LEAVED", _this.prevUrl);
});
}).then(function() {
if (!hasPrev) {
return;
}
return _this.pushDefer(typeof _this.afterEachLeave === "function" ? _this.afterEachLeave.apply(_this, [_args]) : void 0);
}).then(function() {
return _this.pushDefer(typeof _this.beforeEachEnter === "function" ? _this.beforeEachEnter.apply(_this, [_args]) : void 0);
}).then(function() {
var d;
_this.log(" ENTERING", url);
d = _this.pushDefer(c != null ? typeof c.enter === "function" ? c.enter.apply(c, [_args]) : void 0 : void 0);
return d.done(function() {
return _this.log(" ENTERED", url);
});
}).then(function() {
var d;
d = _this.pushDefer(typeof _this.afterEachEnter === "function" ? _this.afterEachEnter.apply(_this, [_args]) : void 0);
_this.prevObj = null;
_this.prevUrl = url;
return d;
});
return this.prevDefer = defer.done(function() {
_this.prevObj = c;
_this.visited.push(_args);
return _this.log("------- ACTIVATED " + url + "\n\n");
});
},
interrupt: function() {
this.defs = _.filter(this.defs, function(i) {
return (i != null ? typeof i.state === "function" ? i.state() : void 0 : void 0) === 'pending';
});
if (this.defs.length === 0) {
return;
}
this.log("------ Interrupted " + this.prevUrl, this.defs.map(function(i) {
return i.name + '::' + i.state();
}));
_.each(this.defs, function(j) {
return j.reject();
});
this.prevObj = null;
return this.prevDefer = new Deferred().resolve();
}
});
})(window, document, jQuery, Backbone);
}).call(this);