tent-status 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/.kick +8 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +232 -0
- data/LICENSE.txt +22 -0
- data/Procfile +1 -0
- data/README.md +14 -0
- data/Rakefile +43 -0
- data/assets/images/.gitkeep +0 -0
- data/assets/images/chosen-sprite.png +0 -0
- data/assets/images/glyphicons-halflings-white.png +0 -0
- data/assets/images/glyphicons-halflings.png +0 -0
- data/assets/javascripts/.gitkeep +0 -0
- data/assets/javascripts/application.js.coffee +122 -0
- data/assets/javascripts/backbone.js +1443 -0
- data/assets/javascripts/backbone_sync.js.coffee +60 -0
- data/assets/javascripts/boot.js.coffee +3 -0
- data/assets/javascripts/chosen.jquery.js +1129 -0
- data/assets/javascripts/collections/.gitkeep +0 -0
- data/assets/javascripts/collections/followers.js.coffee +5 -0
- data/assets/javascripts/collections/followings.js.coffee +5 -0
- data/assets/javascripts/collections/groups.js.coffee +5 -0
- data/assets/javascripts/collections/posts.js.coffee +5 -0
- data/assets/javascripts/fetch_pool.js.coffee +25 -0
- data/assets/javascripts/helpers/.gitkeep +0 -0
- data/assets/javascripts/helpers/formatting.js.coffee +17 -0
- data/assets/javascripts/hogan.js +706 -0
- data/assets/javascripts/http.js.coffee +18 -0
- data/assets/javascripts/jquery.js +9301 -0
- data/assets/javascripts/models/.gitkeep +0 -0
- data/assets/javascripts/models/follower.js.coffee +27 -0
- data/assets/javascripts/models/following.js.coffee +27 -0
- data/assets/javascripts/models/group.js.coffee +4 -0
- data/assets/javascripts/models/post.js.coffee +32 -0
- data/assets/javascripts/models/profile.js.coffee +30 -0
- data/assets/javascripts/moment.js +1106 -0
- data/assets/javascripts/paginator.js.coffee +67 -0
- data/assets/javascripts/router.js.coffee +71 -0
- data/assets/javascripts/routers/.gitkeep +0 -0
- data/assets/javascripts/routers/followers.js.coffee +14 -0
- data/assets/javascripts/routers/followings.js.coffee +14 -0
- data/assets/javascripts/routers/posts.js.coffee +98 -0
- data/assets/javascripts/templates/.gitkeep +0 -0
- data/assets/javascripts/templates/_follower.js.mustache.slim +17 -0
- data/assets/javascripts/templates/_following.js.mustache.slim +17 -0
- data/assets/javascripts/templates/_new_post_form.js.mustache.slim +10 -0
- data/assets/javascripts/templates/_post.js.mustache.slim +10 -0
- data/assets/javascripts/templates/_post_inner.js.mustache.slim +71 -0
- data/assets/javascripts/templates/_profile_stats.js.mustache.slim +11 -0
- data/assets/javascripts/templates/_reply_form.js.mustache.slim +13 -0
- data/assets/javascripts/templates/conversation.js.mustache.slim +13 -0
- data/assets/javascripts/templates/followers.js.mustache.slim +19 -0
- data/assets/javascripts/templates/followings.js.mustache.slim +22 -0
- data/assets/javascripts/templates/posts.js.mustache.slim +25 -0
- data/assets/javascripts/templates/profile.js.mustache.slim +39 -0
- data/assets/javascripts/underscore.js +1059 -0
- data/assets/javascripts/view.js.coffee +140 -0
- data/assets/javascripts/views/.gitkeep +0 -0
- data/assets/javascripts/views/container.js.coffee +6 -0
- data/assets/javascripts/views/expanding_textarea.js.coffee +14 -0
- data/assets/javascripts/views/fetch_posts_pool.js.coffee +61 -0
- data/assets/javascripts/views/follower_groups_form.js.coffee +26 -0
- data/assets/javascripts/views/followers.js.coffee +28 -0
- data/assets/javascripts/views/following_groups_form.js.coffee +25 -0
- data/assets/javascripts/views/followings.js.coffee +27 -0
- data/assets/javascripts/views/new_following_form.js.coffee +15 -0
- data/assets/javascripts/views/new_post_form.js.coffee +178 -0
- data/assets/javascripts/views/post.js.coffee +139 -0
- data/assets/javascripts/views/posts.js.coffee +55 -0
- data/assets/javascripts/views/posts/conversation.js.coffee +18 -0
- data/assets/javascripts/views/profile.js.coffee +29 -0
- data/assets/javascripts/views/profile_follow_button.js.coffee +29 -0
- data/assets/javascripts/views/profile_stats.js.coffee +38 -0
- data/assets/javascripts/views/remove_follower_btn.js.coffee +18 -0
- data/assets/javascripts/views/reply_post_form.js.coffee +30 -0
- data/assets/javascripts/views/unfollow_btn.js.coffee +18 -0
- data/assets/stylesheets/.gitkeep +0 -0
- data/assets/stylesheets/application.css.sass +117 -0
- data/assets/stylesheets/bootstrap-responsive.css +1040 -0
- data/assets/stylesheets/bootstrap.css.erb +5624 -0
- data/assets/stylesheets/chosen.css.erb +397 -0
- data/config.ru +14 -0
- data/config/asset_sync.rb +12 -0
- data/config/evergreen.rb +16 -0
- data/lib/tent-status.rb +6 -0
- data/lib/tent-status/app.rb +263 -0
- data/lib/tent-status/models/user.rb +39 -0
- data/lib/tent-status/sprockets/environment.rb +25 -0
- data/lib/tent-status/sprockets/helpers.rb +5 -0
- data/lib/tent-status/views/application.slim +69 -0
- data/lib/tent-status/views/auth.slim +8 -0
- data/tent-status.gemspec +34 -0
- metadata +415 -0
@@ -0,0 +1,1443 @@
|
|
1
|
+
// Backbone.js 0.9.2
|
2
|
+
|
3
|
+
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
4
|
+
// Backbone may be freely distributed under the MIT license.
|
5
|
+
// For all details and documentation:
|
6
|
+
// http://backbonejs.org
|
7
|
+
|
8
|
+
(function(){
|
9
|
+
|
10
|
+
// Initial Setup
|
11
|
+
// -------------
|
12
|
+
|
13
|
+
// Save a reference to the global object (`window` in the browser, `global`
|
14
|
+
// on the server).
|
15
|
+
var root = this;
|
16
|
+
|
17
|
+
// Save the previous value of the `Backbone` variable, so that it can be
|
18
|
+
// restored later on, if `noConflict` is used.
|
19
|
+
var previousBackbone = root.Backbone;
|
20
|
+
|
21
|
+
// Create a local reference to slice/splice.
|
22
|
+
var slice = Array.prototype.slice;
|
23
|
+
var splice = Array.prototype.splice;
|
24
|
+
|
25
|
+
// The top-level namespace. All public Backbone classes and modules will
|
26
|
+
// be attached to this. Exported for both CommonJS and the browser.
|
27
|
+
var Backbone;
|
28
|
+
if (typeof exports !== 'undefined') {
|
29
|
+
Backbone = exports;
|
30
|
+
} else {
|
31
|
+
Backbone = root.Backbone = {};
|
32
|
+
}
|
33
|
+
|
34
|
+
// Current version of the library. Keep in sync with `package.json`.
|
35
|
+
Backbone.VERSION = '0.9.2';
|
36
|
+
|
37
|
+
// Require Underscore, if we're on the server, and it's not already present.
|
38
|
+
var _ = root._;
|
39
|
+
if (!_ && (typeof require !== 'undefined')) _ = require('underscore');
|
40
|
+
|
41
|
+
// For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
|
42
|
+
var $ = root.jQuery || root.Zepto || root.ender;
|
43
|
+
|
44
|
+
// Set the JavaScript library that will be used for DOM manipulation and
|
45
|
+
// Ajax calls (a.k.a. the `$` variable). By default Backbone will use: jQuery,
|
46
|
+
// Zepto, or Ender; but the `setDomLibrary()` method lets you inject an
|
47
|
+
// alternate JavaScript library (or a mock library for testing your views
|
48
|
+
// outside of a browser).
|
49
|
+
Backbone.setDomLibrary = function(lib) {
|
50
|
+
$ = lib;
|
51
|
+
};
|
52
|
+
|
53
|
+
// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
|
54
|
+
// to its previous owner. Returns a reference to this Backbone object.
|
55
|
+
Backbone.noConflict = function() {
|
56
|
+
root.Backbone = previousBackbone;
|
57
|
+
return this;
|
58
|
+
};
|
59
|
+
|
60
|
+
// Turn on `emulateHTTP` to support legacy HTTP servers. Setting this option
|
61
|
+
// will fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and
|
62
|
+
// set a `X-Http-Method-Override` header.
|
63
|
+
Backbone.emulateHTTP = false;
|
64
|
+
|
65
|
+
// Turn on `emulateJSON` to support legacy servers that can't deal with direct
|
66
|
+
// `application/json` requests ... will encode the body as
|
67
|
+
// `application/x-www-form-urlencoded` instead and will send the model in a
|
68
|
+
// form param named `model`.
|
69
|
+
Backbone.emulateJSON = false;
|
70
|
+
|
71
|
+
// Backbone.Events
|
72
|
+
// -----------------
|
73
|
+
|
74
|
+
// Regular expression used to split event strings
|
75
|
+
var eventSplitter = /\s+/;
|
76
|
+
|
77
|
+
// A module that can be mixed in to *any object* in order to provide it with
|
78
|
+
// custom events. You may bind with `on` or remove with `off` callback functions
|
79
|
+
// to an event; trigger`-ing an event fires all callbacks in succession.
|
80
|
+
//
|
81
|
+
// var object = {};
|
82
|
+
// _.extend(object, Backbone.Events);
|
83
|
+
// object.on('expand', function(){ alert('expanded'); });
|
84
|
+
// object.trigger('expand');
|
85
|
+
//
|
86
|
+
var Events = Backbone.Events = {
|
87
|
+
|
88
|
+
// Bind one or more space separated events, `events`, to a `callback`
|
89
|
+
// function. Passing `"all"` will bind the callback to all events fired.
|
90
|
+
on: function(events, callback, context) {
|
91
|
+
|
92
|
+
var calls, event, node, tail, list;
|
93
|
+
if (!callback) return this;
|
94
|
+
events = events.split(eventSplitter);
|
95
|
+
calls = this._callbacks || (this._callbacks = {});
|
96
|
+
|
97
|
+
// Create an immutable callback list, allowing traversal during
|
98
|
+
// modification. The tail is an empty object that will always be used
|
99
|
+
// as the next node.
|
100
|
+
while (event = events.shift()) {
|
101
|
+
list = calls[event];
|
102
|
+
node = list ? list.tail : {};
|
103
|
+
node.next = tail = {};
|
104
|
+
node.context = context;
|
105
|
+
node.callback = callback;
|
106
|
+
calls[event] = {tail: tail, next: list ? list.next : node};
|
107
|
+
}
|
108
|
+
|
109
|
+
return this;
|
110
|
+
},
|
111
|
+
|
112
|
+
// Custom function, not shipped with backbone
|
113
|
+
once: function(events, callback, context) {
|
114
|
+
var self = this;
|
115
|
+
var _callback = function() {
|
116
|
+
Backbone.Events.off.apply(self, [events, arguments.callee, context]);
|
117
|
+
callback.apply(this, arguments);
|
118
|
+
}
|
119
|
+
var args = [events, _callback, context]
|
120
|
+
Backbone.Events.on.apply(self, args);
|
121
|
+
return self
|
122
|
+
},
|
123
|
+
|
124
|
+
// Remove one or many callbacks. If `context` is null, removes all callbacks
|
125
|
+
// with that function. If `callback` is null, removes all callbacks for the
|
126
|
+
// event. If `events` is null, removes all bound callbacks for all events.
|
127
|
+
off: function(events, callback, context) {
|
128
|
+
var event, calls, node, tail, cb, ctx;
|
129
|
+
|
130
|
+
// No events, or removing *all* events.
|
131
|
+
if (!(calls = this._callbacks)) return;
|
132
|
+
if (!(events || callback || context)) {
|
133
|
+
delete this._callbacks;
|
134
|
+
return this;
|
135
|
+
}
|
136
|
+
|
137
|
+
// Loop through the listed events and contexts, splicing them out of the
|
138
|
+
// linked list of callbacks if appropriate.
|
139
|
+
events = events ? events.split(eventSplitter) : _.keys(calls);
|
140
|
+
while (event = events.shift()) {
|
141
|
+
node = calls[event];
|
142
|
+
delete calls[event];
|
143
|
+
if (!node || !(callback || context)) continue;
|
144
|
+
// Create a new list, omitting the indicated callbacks.
|
145
|
+
tail = node.tail;
|
146
|
+
while ((node = node.next) !== tail) {
|
147
|
+
cb = node.callback;
|
148
|
+
ctx = node.context;
|
149
|
+
if ((callback && cb !== callback) || (context && ctx !== context)) {
|
150
|
+
this.on(event, cb, ctx);
|
151
|
+
}
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
return this;
|
156
|
+
},
|
157
|
+
|
158
|
+
// Trigger one or many events, firing all bound callbacks. Callbacks are
|
159
|
+
// passed the same arguments as `trigger` is, apart from the event name
|
160
|
+
// (unless you're listening on `"all"`, which will cause your callback to
|
161
|
+
// receive the true name of the event as the first argument).
|
162
|
+
trigger: function(events) {
|
163
|
+
var event, node, calls, tail, args, all, rest;
|
164
|
+
if (!(calls = this._callbacks)) return this;
|
165
|
+
all = calls.all;
|
166
|
+
events = events.split(eventSplitter);
|
167
|
+
rest = slice.call(arguments, 1);
|
168
|
+
|
169
|
+
// For each event, walk through the linked list of callbacks twice,
|
170
|
+
// first to trigger the event, then to trigger any `"all"` callbacks.
|
171
|
+
while (event = events.shift()) {
|
172
|
+
if (node = calls[event]) {
|
173
|
+
tail = node.tail;
|
174
|
+
while ((node = node.next) !== tail) {
|
175
|
+
node.callback.apply(node.context || this, rest);
|
176
|
+
}
|
177
|
+
}
|
178
|
+
if (node = all) {
|
179
|
+
tail = node.tail;
|
180
|
+
args = [event].concat(rest);
|
181
|
+
while ((node = node.next) !== tail) {
|
182
|
+
node.callback.apply(node.context || this, args);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
}
|
186
|
+
|
187
|
+
return this;
|
188
|
+
}
|
189
|
+
|
190
|
+
};
|
191
|
+
|
192
|
+
// Aliases for backwards compatibility.
|
193
|
+
Events.bind = Events.on;
|
194
|
+
Events.unbind = Events.off;
|
195
|
+
|
196
|
+
// Backbone.Model
|
197
|
+
// --------------
|
198
|
+
|
199
|
+
// Create a new model, with defined attributes. A client id (`cid`)
|
200
|
+
// is automatically generated and assigned for you.
|
201
|
+
var Model = Backbone.Model = function(attributes, options) {
|
202
|
+
var defaults;
|
203
|
+
attributes || (attributes = {});
|
204
|
+
if (options && options.parse) attributes = this.parse(attributes);
|
205
|
+
if (defaults = getValue(this, 'defaults')) {
|
206
|
+
attributes = _.extend({}, defaults, attributes);
|
207
|
+
}
|
208
|
+
if (options && options.collection) this.collection = options.collection;
|
209
|
+
this.attributes = {};
|
210
|
+
this._escapedAttributes = {};
|
211
|
+
this.cid = _.uniqueId('c');
|
212
|
+
this.changed = {};
|
213
|
+
this._silent = {};
|
214
|
+
this._pending = {};
|
215
|
+
this.set(attributes, {silent: true});
|
216
|
+
// Reset change tracking.
|
217
|
+
this.changed = {};
|
218
|
+
this._silent = {};
|
219
|
+
this._pending = {};
|
220
|
+
this._previousAttributes = _.clone(this.attributes);
|
221
|
+
this.initialize.apply(this, arguments);
|
222
|
+
};
|
223
|
+
|
224
|
+
// Attach all inheritable methods to the Model prototype.
|
225
|
+
_.extend(Model.prototype, Events, {
|
226
|
+
|
227
|
+
// A hash of attributes whose current and previous value differ.
|
228
|
+
changed: null,
|
229
|
+
|
230
|
+
// A hash of attributes that have silently changed since the last time
|
231
|
+
// `change` was called. Will become pending attributes on the next call.
|
232
|
+
_silent: null,
|
233
|
+
|
234
|
+
// A hash of attributes that have changed since the last `'change'` event
|
235
|
+
// began.
|
236
|
+
_pending: null,
|
237
|
+
|
238
|
+
// The default name for the JSON `id` attribute is `"id"`. MongoDB and
|
239
|
+
// CouchDB users may want to set this to `"_id"`.
|
240
|
+
idAttribute: 'id',
|
241
|
+
|
242
|
+
// Initialize is an empty function by default. Override it with your own
|
243
|
+
// initialization logic.
|
244
|
+
initialize: function(){},
|
245
|
+
|
246
|
+
// Return a copy of the model's `attributes` object.
|
247
|
+
toJSON: function(options) {
|
248
|
+
return _.clone(this.attributes);
|
249
|
+
},
|
250
|
+
|
251
|
+
// Get the value of an attribute.
|
252
|
+
get: function(attr) {
|
253
|
+
return this.attributes[attr];
|
254
|
+
},
|
255
|
+
|
256
|
+
// Get the HTML-escaped value of an attribute.
|
257
|
+
escape: function(attr) {
|
258
|
+
var html;
|
259
|
+
if (html = this._escapedAttributes[attr]) return html;
|
260
|
+
var val = this.get(attr);
|
261
|
+
return this._escapedAttributes[attr] = _.escape(val == null ? '' : '' + val);
|
262
|
+
},
|
263
|
+
|
264
|
+
// Returns `true` if the attribute contains a value that is not null
|
265
|
+
// or undefined.
|
266
|
+
has: function(attr) {
|
267
|
+
return this.get(attr) != null;
|
268
|
+
},
|
269
|
+
|
270
|
+
// Set a hash of model attributes on the object, firing `"change"` unless
|
271
|
+
// you choose to silence it.
|
272
|
+
set: function(key, value, options) {
|
273
|
+
var attrs, attr, val;
|
274
|
+
|
275
|
+
// Handle both `"key", value` and `{key: value}` -style arguments.
|
276
|
+
if (_.isObject(key) || key == null) {
|
277
|
+
attrs = key;
|
278
|
+
options = value;
|
279
|
+
} else {
|
280
|
+
attrs = {};
|
281
|
+
attrs[key] = value;
|
282
|
+
}
|
283
|
+
|
284
|
+
// Extract attributes and options.
|
285
|
+
options || (options = {});
|
286
|
+
if (!attrs) return this;
|
287
|
+
if (attrs instanceof Model) attrs = attrs.attributes;
|
288
|
+
if (options.unset) for (attr in attrs) attrs[attr] = void 0;
|
289
|
+
|
290
|
+
// Run validation.
|
291
|
+
if (!this._validate(attrs, options)) return false;
|
292
|
+
|
293
|
+
// Check for changes of `id`.
|
294
|
+
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
|
295
|
+
|
296
|
+
var changes = options.changes = {};
|
297
|
+
var now = this.attributes;
|
298
|
+
var escaped = this._escapedAttributes;
|
299
|
+
var prev = this._previousAttributes || {};
|
300
|
+
|
301
|
+
// For each `set` attribute...
|
302
|
+
for (attr in attrs) {
|
303
|
+
val = attrs[attr];
|
304
|
+
|
305
|
+
// If the new and current value differ, record the change.
|
306
|
+
if (!_.isEqual(now[attr], val) || (options.unset && _.has(now, attr))) {
|
307
|
+
delete escaped[attr];
|
308
|
+
(options.silent ? this._silent : changes)[attr] = true;
|
309
|
+
}
|
310
|
+
|
311
|
+
// Update or delete the current value.
|
312
|
+
options.unset ? delete now[attr] : now[attr] = val;
|
313
|
+
|
314
|
+
// If the new and previous value differ, record the change. If not,
|
315
|
+
// then remove changes for this attribute.
|
316
|
+
if (!_.isEqual(prev[attr], val) || (_.has(now, attr) != _.has(prev, attr))) {
|
317
|
+
this.changed[attr] = val;
|
318
|
+
if (!options.silent) this._pending[attr] = true;
|
319
|
+
} else {
|
320
|
+
delete this.changed[attr];
|
321
|
+
delete this._pending[attr];
|
322
|
+
}
|
323
|
+
}
|
324
|
+
|
325
|
+
// Fire the `"change"` events.
|
326
|
+
if (!options.silent) this.change(options);
|
327
|
+
return this;
|
328
|
+
},
|
329
|
+
|
330
|
+
// Remove an attribute from the model, firing `"change"` unless you choose
|
331
|
+
// to silence it. `unset` is a noop if the attribute doesn't exist.
|
332
|
+
unset: function(attr, options) {
|
333
|
+
(options || (options = {})).unset = true;
|
334
|
+
return this.set(attr, null, options);
|
335
|
+
},
|
336
|
+
|
337
|
+
// Clear all attributes on the model, firing `"change"` unless you choose
|
338
|
+
// to silence it.
|
339
|
+
clear: function(options) {
|
340
|
+
(options || (options = {})).unset = true;
|
341
|
+
return this.set(_.clone(this.attributes), options);
|
342
|
+
},
|
343
|
+
|
344
|
+
// Fetch the model from the server. If the server's representation of the
|
345
|
+
// model differs from its current attributes, they will be overriden,
|
346
|
+
// triggering a `"change"` event.
|
347
|
+
fetch: function(options) {
|
348
|
+
options = options ? _.clone(options) : {};
|
349
|
+
var model = this;
|
350
|
+
var success = options.success;
|
351
|
+
options.success = function(resp, status, xhr) {
|
352
|
+
if (!model.set(model.parse(resp, xhr), options)) return false;
|
353
|
+
if (success) success(model, resp);
|
354
|
+
};
|
355
|
+
options.error = Backbone.wrapError(options.error, model, options);
|
356
|
+
return (this.sync || Backbone.sync).call(this, 'read', this, options);
|
357
|
+
},
|
358
|
+
|
359
|
+
// Set a hash of model attributes, and sync the model to the server.
|
360
|
+
// If the server returns an attributes hash that differs, the model's
|
361
|
+
// state will be `set` again.
|
362
|
+
save: function(key, value, options) {
|
363
|
+
var attrs, current;
|
364
|
+
|
365
|
+
// Handle both `("key", value)` and `({key: value})` -style calls.
|
366
|
+
if (_.isObject(key) || key == null) {
|
367
|
+
attrs = key;
|
368
|
+
options = value;
|
369
|
+
} else {
|
370
|
+
attrs = {};
|
371
|
+
attrs[key] = value;
|
372
|
+
}
|
373
|
+
options = options ? _.clone(options) : {};
|
374
|
+
|
375
|
+
// If we're "wait"-ing to set changed attributes, validate early.
|
376
|
+
if (options.wait) {
|
377
|
+
if (!this._validate(attrs, options)) return false;
|
378
|
+
current = _.clone(this.attributes);
|
379
|
+
}
|
380
|
+
|
381
|
+
// Regular saves `set` attributes before persisting to the server.
|
382
|
+
var silentOptions = _.extend({}, options, {silent: true});
|
383
|
+
if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
|
384
|
+
return false;
|
385
|
+
}
|
386
|
+
|
387
|
+
// After a successful server-side save, the client is (optionally)
|
388
|
+
// updated with the server-side state.
|
389
|
+
var model = this;
|
390
|
+
var success = options.success;
|
391
|
+
options.success = function(resp, status, xhr) {
|
392
|
+
var serverAttrs = model.parse(resp, xhr);
|
393
|
+
if (options.wait) {
|
394
|
+
delete options.wait;
|
395
|
+
serverAttrs = _.extend(attrs || {}, serverAttrs);
|
396
|
+
}
|
397
|
+
if (!model.set(serverAttrs, options)) return false;
|
398
|
+
if (success) {
|
399
|
+
success(model, resp);
|
400
|
+
} else {
|
401
|
+
model.trigger('sync', model, resp, options);
|
402
|
+
}
|
403
|
+
};
|
404
|
+
|
405
|
+
// Finish configuring and sending the Ajax request.
|
406
|
+
options.error = Backbone.wrapError(options.error, model, options);
|
407
|
+
var method = this.isNew() ? 'create' : 'update';
|
408
|
+
var xhr = (this.sync || Backbone.sync).call(this, method, this, options);
|
409
|
+
if (options.wait) this.set(current, silentOptions);
|
410
|
+
return xhr;
|
411
|
+
},
|
412
|
+
|
413
|
+
// Destroy this model on the server if it was already persisted.
|
414
|
+
// Optimistically removes the model from its collection, if it has one.
|
415
|
+
// If `wait: true` is passed, waits for the server to respond before removal.
|
416
|
+
destroy: function(options) {
|
417
|
+
options = options ? _.clone(options) : {};
|
418
|
+
var model = this;
|
419
|
+
var success = options.success;
|
420
|
+
|
421
|
+
var triggerDestroy = function() {
|
422
|
+
model.trigger('destroy', model, model.collection, options);
|
423
|
+
};
|
424
|
+
|
425
|
+
if (this.isNew()) {
|
426
|
+
triggerDestroy();
|
427
|
+
return false;
|
428
|
+
}
|
429
|
+
|
430
|
+
options.success = function(resp) {
|
431
|
+
if (options.wait) triggerDestroy();
|
432
|
+
if (success) {
|
433
|
+
success(model, resp);
|
434
|
+
} else {
|
435
|
+
model.trigger('sync', model, resp, options);
|
436
|
+
}
|
437
|
+
};
|
438
|
+
|
439
|
+
options.error = Backbone.wrapError(options.error, model, options);
|
440
|
+
var xhr = (this.sync || Backbone.sync).call(this, 'delete', this, options);
|
441
|
+
if (!options.wait) triggerDestroy();
|
442
|
+
return xhr;
|
443
|
+
},
|
444
|
+
|
445
|
+
// Default URL for the model's representation on the server -- if you're
|
446
|
+
// using Backbone's restful methods, override this to change the endpoint
|
447
|
+
// that will be called.
|
448
|
+
url: function() {
|
449
|
+
var base = getValue(this, 'urlRoot') || getValue(this.collection, 'url') || urlError();
|
450
|
+
if (this.isNew()) return base;
|
451
|
+
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
|
452
|
+
},
|
453
|
+
|
454
|
+
// **parse** converts a response into the hash of attributes to be `set` on
|
455
|
+
// the model. The default implementation is just to pass the response along.
|
456
|
+
parse: function(resp, xhr) {
|
457
|
+
return resp;
|
458
|
+
},
|
459
|
+
|
460
|
+
// Create a new model with identical attributes to this one.
|
461
|
+
clone: function() {
|
462
|
+
return new this.constructor(this.attributes);
|
463
|
+
},
|
464
|
+
|
465
|
+
// A model is new if it has never been saved to the server, and lacks an id.
|
466
|
+
isNew: function() {
|
467
|
+
return this.id == null;
|
468
|
+
},
|
469
|
+
|
470
|
+
// Call this method to manually fire a `"change"` event for this model and
|
471
|
+
// a `"change:attribute"` event for each changed attribute.
|
472
|
+
// Calling this will cause all objects observing the model to update.
|
473
|
+
change: function(options) {
|
474
|
+
options || (options = {});
|
475
|
+
var changing = this._changing;
|
476
|
+
this._changing = true;
|
477
|
+
|
478
|
+
// Silent changes become pending changes.
|
479
|
+
for (var attr in this._silent) this._pending[attr] = true;
|
480
|
+
|
481
|
+
// Silent changes are triggered.
|
482
|
+
var changes = _.extend({}, options.changes, this._silent);
|
483
|
+
this._silent = {};
|
484
|
+
for (var attr in changes) {
|
485
|
+
this.trigger('change:' + attr, this, this.get(attr), options);
|
486
|
+
}
|
487
|
+
if (changing) return this;
|
488
|
+
|
489
|
+
// Continue firing `"change"` events while there are pending changes.
|
490
|
+
while (!_.isEmpty(this._pending)) {
|
491
|
+
this._pending = {};
|
492
|
+
this.trigger('change', this, options);
|
493
|
+
// Pending and silent changes still remain.
|
494
|
+
for (var attr in this.changed) {
|
495
|
+
if (this._pending[attr] || this._silent[attr]) continue;
|
496
|
+
delete this.changed[attr];
|
497
|
+
}
|
498
|
+
this._previousAttributes = _.clone(this.attributes);
|
499
|
+
}
|
500
|
+
|
501
|
+
this._changing = false;
|
502
|
+
return this;
|
503
|
+
},
|
504
|
+
|
505
|
+
// Determine if the model has changed since the last `"change"` event.
|
506
|
+
// If you specify an attribute name, determine if that attribute has changed.
|
507
|
+
hasChanged: function(attr) {
|
508
|
+
if (!arguments.length) return !_.isEmpty(this.changed);
|
509
|
+
return _.has(this.changed, attr);
|
510
|
+
},
|
511
|
+
|
512
|
+
// Return an object containing all the attributes that have changed, or
|
513
|
+
// false if there are no changed attributes. Useful for determining what
|
514
|
+
// parts of a view need to be updated and/or what attributes need to be
|
515
|
+
// persisted to the server. Unset attributes will be set to undefined.
|
516
|
+
// You can also pass an attributes object to diff against the model,
|
517
|
+
// determining if there *would be* a change.
|
518
|
+
changedAttributes: function(diff) {
|
519
|
+
if (!diff) return this.hasChanged() ? _.clone(this.changed) : false;
|
520
|
+
var val, changed = false, old = this._previousAttributes;
|
521
|
+
for (var attr in diff) {
|
522
|
+
if (_.isEqual(old[attr], (val = diff[attr]))) continue;
|
523
|
+
(changed || (changed = {}))[attr] = val;
|
524
|
+
}
|
525
|
+
return changed;
|
526
|
+
},
|
527
|
+
|
528
|
+
// Get the previous value of an attribute, recorded at the time the last
|
529
|
+
// `"change"` event was fired.
|
530
|
+
previous: function(attr) {
|
531
|
+
if (!arguments.length || !this._previousAttributes) return null;
|
532
|
+
return this._previousAttributes[attr];
|
533
|
+
},
|
534
|
+
|
535
|
+
// Get all of the attributes of the model at the time of the previous
|
536
|
+
// `"change"` event.
|
537
|
+
previousAttributes: function() {
|
538
|
+
return _.clone(this._previousAttributes);
|
539
|
+
},
|
540
|
+
|
541
|
+
// Check if the model is currently in a valid state. It's only possible to
|
542
|
+
// get into an *invalid* state if you're using silent changes.
|
543
|
+
isValid: function() {
|
544
|
+
return !this.validate(this.attributes);
|
545
|
+
},
|
546
|
+
|
547
|
+
// Run validation against the next complete set of model attributes,
|
548
|
+
// returning `true` if all is well. If a specific `error` callback has
|
549
|
+
// been passed, call that instead of firing the general `"error"` event.
|
550
|
+
_validate: function(attrs, options) {
|
551
|
+
if (options.silent || !this.validate) return true;
|
552
|
+
attrs = _.extend({}, this.attributes, attrs);
|
553
|
+
var error = this.validate(attrs, options);
|
554
|
+
if (!error) return true;
|
555
|
+
if (options && options.error) {
|
556
|
+
options.error(this, error, options);
|
557
|
+
} else {
|
558
|
+
this.trigger('error', this, error, options);
|
559
|
+
}
|
560
|
+
return false;
|
561
|
+
}
|
562
|
+
|
563
|
+
});
|
564
|
+
|
565
|
+
// Backbone.Collection
|
566
|
+
// -------------------
|
567
|
+
|
568
|
+
// Provides a standard collection class for our sets of models, ordered
|
569
|
+
// or unordered. If a `comparator` is specified, the Collection will maintain
|
570
|
+
// its models in sort order, as they're added and removed.
|
571
|
+
var Collection = Backbone.Collection = function(models, options) {
|
572
|
+
options || (options = {});
|
573
|
+
if (options.model) this.model = options.model;
|
574
|
+
if (options.comparator) this.comparator = options.comparator;
|
575
|
+
this._reset();
|
576
|
+
this.initialize.apply(this, arguments);
|
577
|
+
if (models) this.reset(models, {silent: true, parse: options.parse});
|
578
|
+
};
|
579
|
+
|
580
|
+
// Define the Collection's inheritable methods.
|
581
|
+
_.extend(Collection.prototype, Events, {
|
582
|
+
|
583
|
+
// The default model for a collection is just a **Backbone.Model**.
|
584
|
+
// This should be overridden in most cases.
|
585
|
+
model: Model,
|
586
|
+
|
587
|
+
// Initialize is an empty function by default. Override it with your own
|
588
|
+
// initialization logic.
|
589
|
+
initialize: function(){},
|
590
|
+
|
591
|
+
// The JSON representation of a Collection is an array of the
|
592
|
+
// models' attributes.
|
593
|
+
toJSON: function(options) {
|
594
|
+
return this.map(function(model){ return model.toJSON(options); });
|
595
|
+
},
|
596
|
+
|
597
|
+
// Add a model, or list of models to the set. Pass **silent** to avoid
|
598
|
+
// firing the `add` event for every new model.
|
599
|
+
add: function(models, options) {
|
600
|
+
var i, index, length, model, cid, id, cids = {}, ids = {}, dups = [];
|
601
|
+
options || (options = {});
|
602
|
+
models = _.isArray(models) ? models.slice() : [models];
|
603
|
+
|
604
|
+
// Begin by turning bare objects into model references, and preventing
|
605
|
+
// invalid models or duplicate models from being added.
|
606
|
+
for (i = 0, length = models.length; i < length; i++) {
|
607
|
+
if (!(model = models[i] = this._prepareModel(models[i], options))) {
|
608
|
+
throw new Error("Can't add an invalid model to a collection");
|
609
|
+
}
|
610
|
+
cid = model.cid;
|
611
|
+
id = model.id;
|
612
|
+
if (cids[cid] || this._byCid[cid] || ((id != null) && (ids[id] || this._byId[id]))) {
|
613
|
+
dups.push(i);
|
614
|
+
continue;
|
615
|
+
}
|
616
|
+
cids[cid] = ids[id] = model;
|
617
|
+
}
|
618
|
+
|
619
|
+
// Remove duplicates.
|
620
|
+
i = dups.length;
|
621
|
+
while (i--) {
|
622
|
+
models.splice(dups[i], 1);
|
623
|
+
}
|
624
|
+
|
625
|
+
// Listen to added models' events, and index models for lookup by
|
626
|
+
// `id` and by `cid`.
|
627
|
+
for (i = 0, length = models.length; i < length; i++) {
|
628
|
+
(model = models[i]).on('all', this._onModelEvent, this);
|
629
|
+
this._byCid[model.cid] = model;
|
630
|
+
if (model.id != null) this._byId[model.id] = model;
|
631
|
+
}
|
632
|
+
|
633
|
+
// Insert models into the collection, re-sorting if needed, and triggering
|
634
|
+
// `add` events unless silenced.
|
635
|
+
this.length += length;
|
636
|
+
index = options.at != null ? options.at : this.models.length;
|
637
|
+
splice.apply(this.models, [index, 0].concat(models));
|
638
|
+
if (this.comparator) this.sort({silent: true});
|
639
|
+
if (options.silent) return this;
|
640
|
+
for (i = 0, length = this.models.length; i < length; i++) {
|
641
|
+
if (!cids[(model = this.models[i]).cid]) continue;
|
642
|
+
options.index = i;
|
643
|
+
model.trigger('add', model, this, options);
|
644
|
+
}
|
645
|
+
return this;
|
646
|
+
},
|
647
|
+
|
648
|
+
// Remove a model, or a list of models from the set. Pass silent to avoid
|
649
|
+
// firing the `remove` event for every model removed.
|
650
|
+
remove: function(models, options) {
|
651
|
+
var i, l, index, model;
|
652
|
+
options || (options = {});
|
653
|
+
models = _.isArray(models) ? models.slice() : [models];
|
654
|
+
for (i = 0, l = models.length; i < l; i++) {
|
655
|
+
model = this.getByCid(models[i]) || this.get(models[i]);
|
656
|
+
if (!model) continue;
|
657
|
+
delete this._byId[model.id];
|
658
|
+
delete this._byCid[model.cid];
|
659
|
+
index = this.indexOf(model);
|
660
|
+
this.models.splice(index, 1);
|
661
|
+
this.length--;
|
662
|
+
if (!options.silent) {
|
663
|
+
options.index = index;
|
664
|
+
model.trigger('remove', model, this, options);
|
665
|
+
}
|
666
|
+
this._removeReference(model);
|
667
|
+
}
|
668
|
+
return this;
|
669
|
+
},
|
670
|
+
|
671
|
+
// Add a model to the end of the collection.
|
672
|
+
push: function(model, options) {
|
673
|
+
model = this._prepareModel(model, options);
|
674
|
+
this.add(model, options);
|
675
|
+
return model;
|
676
|
+
},
|
677
|
+
|
678
|
+
// Remove a model from the end of the collection.
|
679
|
+
pop: function(options) {
|
680
|
+
var model = this.at(this.length - 1);
|
681
|
+
this.remove(model, options);
|
682
|
+
return model;
|
683
|
+
},
|
684
|
+
|
685
|
+
// Add a model to the beginning of the collection.
|
686
|
+
unshift: function(model, options) {
|
687
|
+
model = this._prepareModel(model, options);
|
688
|
+
this.add(model, _.extend({at: 0}, options));
|
689
|
+
return model;
|
690
|
+
},
|
691
|
+
|
692
|
+
// Remove a model from the beginning of the collection.
|
693
|
+
shift: function(options) {
|
694
|
+
var model = this.at(0);
|
695
|
+
this.remove(model, options);
|
696
|
+
return model;
|
697
|
+
},
|
698
|
+
|
699
|
+
// Get a model from the set by id.
|
700
|
+
get: function(id) {
|
701
|
+
if (id == null) return void 0;
|
702
|
+
return this._byId[id.id != null ? id.id : id];
|
703
|
+
},
|
704
|
+
|
705
|
+
// Get a model from the set by client id.
|
706
|
+
getByCid: function(cid) {
|
707
|
+
return cid && this._byCid[cid.cid || cid];
|
708
|
+
},
|
709
|
+
|
710
|
+
// Get the model at the given index.
|
711
|
+
at: function(index) {
|
712
|
+
return this.models[index];
|
713
|
+
},
|
714
|
+
|
715
|
+
// Return models with matching attributes. Useful for simple cases of `filter`.
|
716
|
+
where: function(attrs) {
|
717
|
+
if (_.isEmpty(attrs)) return [];
|
718
|
+
return this.filter(function(model) {
|
719
|
+
for (var key in attrs) {
|
720
|
+
if (attrs[key] !== model.get(key)) return false;
|
721
|
+
}
|
722
|
+
return true;
|
723
|
+
});
|
724
|
+
},
|
725
|
+
|
726
|
+
// Force the collection to re-sort itself. You don't need to call this under
|
727
|
+
// normal circumstances, as the set will maintain sort order as each item
|
728
|
+
// is added.
|
729
|
+
sort: function(options) {
|
730
|
+
options || (options = {});
|
731
|
+
if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
|
732
|
+
var boundComparator = _.bind(this.comparator, this);
|
733
|
+
if (this.comparator.length == 1) {
|
734
|
+
this.models = this.sortBy(boundComparator);
|
735
|
+
} else {
|
736
|
+
this.models.sort(boundComparator);
|
737
|
+
}
|
738
|
+
if (!options.silent) this.trigger('reset', this, options);
|
739
|
+
return this;
|
740
|
+
},
|
741
|
+
|
742
|
+
// Pluck an attribute from each model in the collection.
|
743
|
+
pluck: function(attr) {
|
744
|
+
return _.map(this.models, function(model){ return model.get(attr); });
|
745
|
+
},
|
746
|
+
|
747
|
+
// When you have more items than you want to add or remove individually,
|
748
|
+
// you can reset the entire set with a new list of models, without firing
|
749
|
+
// any `add` or `remove` events. Fires `reset` when finished.
|
750
|
+
reset: function(models, options) {
|
751
|
+
models || (models = []);
|
752
|
+
options || (options = {});
|
753
|
+
for (var i = 0, l = this.models.length; i < l; i++) {
|
754
|
+
this._removeReference(this.models[i]);
|
755
|
+
}
|
756
|
+
this._reset();
|
757
|
+
this.add(models, _.extend({silent: true}, options));
|
758
|
+
if (!options.silent) this.trigger('reset', this, options);
|
759
|
+
return this;
|
760
|
+
},
|
761
|
+
|
762
|
+
// Fetch the default set of models for this collection, resetting the
|
763
|
+
// collection when they arrive. If `add: true` is passed, appends the
|
764
|
+
// models to the collection instead of resetting.
|
765
|
+
fetch: function(options) {
|
766
|
+
options = options ? _.clone(options) : {};
|
767
|
+
if (options.parse === undefined) options.parse = true;
|
768
|
+
var collection = this;
|
769
|
+
var success = options.success;
|
770
|
+
options.success = function(resp, status, xhr) {
|
771
|
+
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
|
772
|
+
if (success) success(collection, resp);
|
773
|
+
};
|
774
|
+
options.error = Backbone.wrapError(options.error, collection, options);
|
775
|
+
return (this.sync || Backbone.sync).call(this, 'read', this, options);
|
776
|
+
},
|
777
|
+
|
778
|
+
// Create a new instance of a model in this collection. Add the model to the
|
779
|
+
// collection immediately, unless `wait: true` is passed, in which case we
|
780
|
+
// wait for the server to agree.
|
781
|
+
create: function(model, options) {
|
782
|
+
var coll = this;
|
783
|
+
options = options ? _.clone(options) : {};
|
784
|
+
model = this._prepareModel(model, options);
|
785
|
+
if (!model) return false;
|
786
|
+
if (!options.wait) coll.add(model, options);
|
787
|
+
var success = options.success;
|
788
|
+
options.success = function(nextModel, resp, xhr) {
|
789
|
+
if (options.wait) coll.add(nextModel, options);
|
790
|
+
if (success) {
|
791
|
+
success(nextModel, resp);
|
792
|
+
} else {
|
793
|
+
nextModel.trigger('sync', model, resp, options);
|
794
|
+
}
|
795
|
+
};
|
796
|
+
model.save(null, options);
|
797
|
+
return model;
|
798
|
+
},
|
799
|
+
|
800
|
+
// **parse** converts a response into a list of models to be added to the
|
801
|
+
// collection. The default implementation is just to pass it through.
|
802
|
+
parse: function(resp, xhr) {
|
803
|
+
return resp;
|
804
|
+
},
|
805
|
+
|
806
|
+
// Proxy to _'s chain. Can't be proxied the same way the rest of the
|
807
|
+
// underscore methods are proxied because it relies on the underscore
|
808
|
+
// constructor.
|
809
|
+
chain: function () {
|
810
|
+
return _(this.models).chain();
|
811
|
+
},
|
812
|
+
|
813
|
+
// Reset all internal state. Called when the collection is reset.
|
814
|
+
_reset: function(options) {
|
815
|
+
this.length = 0;
|
816
|
+
this.models = [];
|
817
|
+
this._byId = {};
|
818
|
+
this._byCid = {};
|
819
|
+
},
|
820
|
+
|
821
|
+
// Prepare a model or hash of attributes to be added to this collection.
|
822
|
+
_prepareModel: function(model, options) {
|
823
|
+
options || (options = {});
|
824
|
+
if (!(model instanceof Model)) {
|
825
|
+
var attrs = model;
|
826
|
+
options.collection = this;
|
827
|
+
model = new this.model(attrs, options);
|
828
|
+
if (!model._validate(model.attributes, options)) model = false;
|
829
|
+
} else if (!model.collection) {
|
830
|
+
model.collection = this;
|
831
|
+
}
|
832
|
+
return model;
|
833
|
+
},
|
834
|
+
|
835
|
+
// Internal method to remove a model's ties to a collection.
|
836
|
+
_removeReference: function(model) {
|
837
|
+
if (this == model.collection) {
|
838
|
+
delete model.collection;
|
839
|
+
}
|
840
|
+
model.off('all', this._onModelEvent, this);
|
841
|
+
},
|
842
|
+
|
843
|
+
// Internal method called every time a model in the set fires an event.
|
844
|
+
// Sets need to update their indexes when models change ids. All other
|
845
|
+
// events simply proxy through. "add" and "remove" events that originate
|
846
|
+
// in other collections are ignored.
|
847
|
+
_onModelEvent: function(event, model, collection, options) {
|
848
|
+
if ((event == 'add' || event == 'remove') && collection != this) return;
|
849
|
+
if (event == 'destroy') {
|
850
|
+
this.remove(model, options);
|
851
|
+
}
|
852
|
+
if (model && event === 'change:' + model.idAttribute) {
|
853
|
+
delete this._byId[model.previous(model.idAttribute)];
|
854
|
+
this._byId[model.id] = model;
|
855
|
+
}
|
856
|
+
this.trigger.apply(this, arguments);
|
857
|
+
}
|
858
|
+
|
859
|
+
});
|
860
|
+
|
861
|
+
// Underscore methods that we want to implement on the Collection.
|
862
|
+
var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find',
|
863
|
+
'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any',
|
864
|
+
'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex',
|
865
|
+
'toArray', 'size', 'first', 'initial', 'rest', 'last', 'without', 'indexOf',
|
866
|
+
'shuffle', 'lastIndexOf', 'isEmpty', 'groupBy'];
|
867
|
+
|
868
|
+
// Mix in each Underscore method as a proxy to `Collection#models`.
|
869
|
+
_.each(methods, function(method) {
|
870
|
+
Collection.prototype[method] = function() {
|
871
|
+
return _[method].apply(_, [this.models].concat(_.toArray(arguments)));
|
872
|
+
};
|
873
|
+
});
|
874
|
+
|
875
|
+
// Backbone.Router
|
876
|
+
// -------------------
|
877
|
+
|
878
|
+
// Routers map faux-URLs to actions, and fire events when routes are
|
879
|
+
// matched. Creating a new one sets its `routes` hash, if not set statically.
|
880
|
+
var Router = Backbone.Router = function(options) {
|
881
|
+
options || (options = {});
|
882
|
+
if (options.routes) this.routes = options.routes;
|
883
|
+
this._bindRoutes();
|
884
|
+
this.initialize.apply(this, arguments);
|
885
|
+
};
|
886
|
+
|
887
|
+
// Cached regular expressions for matching named param parts and splatted
|
888
|
+
// parts of route strings.
|
889
|
+
var namedParam = /:\w+/g;
|
890
|
+
var splatParam = /\*\w+/g;
|
891
|
+
var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
|
892
|
+
|
893
|
+
// Set up all inheritable **Backbone.Router** properties and methods.
|
894
|
+
_.extend(Router.prototype, Events, {
|
895
|
+
|
896
|
+
// Initialize is an empty function by default. Override it with your own
|
897
|
+
// initialization logic.
|
898
|
+
initialize: function(){},
|
899
|
+
|
900
|
+
// Manually bind a single named route to a callback. For example:
|
901
|
+
//
|
902
|
+
// this.route('search/:query/p:num', 'search', function(query, num) {
|
903
|
+
// ...
|
904
|
+
// });
|
905
|
+
//
|
906
|
+
route: function(route, name, callback) {
|
907
|
+
Backbone.history || (Backbone.history = new History);
|
908
|
+
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
|
909
|
+
if (!callback) callback = this[name];
|
910
|
+
Backbone.history.route(route, _.bind(function(fragment) {
|
911
|
+
var args = this._extractParameters(route, fragment);
|
912
|
+
callback && callback.apply(this, args);
|
913
|
+
this.trigger.apply(this, ['route:' + name].concat(args));
|
914
|
+
Backbone.history.trigger('route', this, name, args);
|
915
|
+
}, this));
|
916
|
+
return this;
|
917
|
+
},
|
918
|
+
|
919
|
+
// Simple proxy to `Backbone.history` to save a fragment into the history.
|
920
|
+
navigate: function(fragment, options) {
|
921
|
+
Backbone.history.navigate(fragment, options);
|
922
|
+
},
|
923
|
+
|
924
|
+
// Bind all defined routes to `Backbone.history`. We have to reverse the
|
925
|
+
// order of the routes here to support behavior where the most general
|
926
|
+
// routes can be defined at the bottom of the route map.
|
927
|
+
_bindRoutes: function() {
|
928
|
+
if (!this.routes) return;
|
929
|
+
var routes = [];
|
930
|
+
for (var route in this.routes) {
|
931
|
+
routes.unshift([route, this.routes[route]]);
|
932
|
+
}
|
933
|
+
for (var i = 0, l = routes.length; i < l; i++) {
|
934
|
+
this.route(routes[i][0], routes[i][1], this[routes[i][1]]);
|
935
|
+
}
|
936
|
+
},
|
937
|
+
|
938
|
+
// Convert a route string into a regular expression, suitable for matching
|
939
|
+
// against the current location hash.
|
940
|
+
_routeToRegExp: function(route) {
|
941
|
+
route = route.replace(escapeRegExp, '\\$&')
|
942
|
+
.replace(namedParam, '([^\/]+)')
|
943
|
+
.replace(splatParam, '(.*?)');
|
944
|
+
return new RegExp('^' + route + '$');
|
945
|
+
},
|
946
|
+
|
947
|
+
// Given a route, and a URL fragment that it matches, return the array of
|
948
|
+
// extracted parameters.
|
949
|
+
_extractParameters: function(route, fragment) {
|
950
|
+
return route.exec(fragment).slice(1);
|
951
|
+
}
|
952
|
+
|
953
|
+
});
|
954
|
+
|
955
|
+
// Backbone.History
|
956
|
+
// ----------------
|
957
|
+
|
958
|
+
// Handles cross-browser history management, based on URL fragments. If the
|
959
|
+
// browser does not support `onhashchange`, falls back to polling.
|
960
|
+
var History = Backbone.History = function() {
|
961
|
+
this.handlers = [];
|
962
|
+
_.bindAll(this, 'checkUrl');
|
963
|
+
};
|
964
|
+
|
965
|
+
// Cached regex for cleaning leading hashes and slashes .
|
966
|
+
var routeStripper = /^[#\/]/;
|
967
|
+
|
968
|
+
// Cached regex for detecting MSIE.
|
969
|
+
var isExplorer = /msie [\w.]+/;
|
970
|
+
|
971
|
+
// Has the history handling already been started?
|
972
|
+
History.started = false;
|
973
|
+
|
974
|
+
// Set up all inheritable **Backbone.History** properties and methods.
|
975
|
+
_.extend(History.prototype, Events, {
|
976
|
+
|
977
|
+
// The default interval to poll for hash changes, if necessary, is
|
978
|
+
// twenty times a second.
|
979
|
+
interval: 50,
|
980
|
+
|
981
|
+
// Gets the true hash value. Cannot use location.hash directly due to bug
|
982
|
+
// in Firefox where location.hash will always be decoded.
|
983
|
+
getHash: function(windowOverride) {
|
984
|
+
var loc = windowOverride ? windowOverride.location : window.location;
|
985
|
+
var match = loc.href.match(/#(.*)$/);
|
986
|
+
return match ? match[1] : '';
|
987
|
+
},
|
988
|
+
|
989
|
+
// Get the cross-browser normalized URL fragment, either from the URL,
|
990
|
+
// the hash, or the override.
|
991
|
+
getFragment: function(fragment, forcePushState) {
|
992
|
+
if (fragment == null) {
|
993
|
+
if (this._hasPushState || forcePushState) {
|
994
|
+
fragment = window.location.pathname;
|
995
|
+
var search = window.location.search;
|
996
|
+
if (search) fragment += search;
|
997
|
+
} else {
|
998
|
+
fragment = this.getHash();
|
999
|
+
}
|
1000
|
+
}
|
1001
|
+
if (!fragment.indexOf(this.options.root)) fragment = fragment.substr(this.options.root.length);
|
1002
|
+
return fragment.replace(routeStripper, '');
|
1003
|
+
},
|
1004
|
+
|
1005
|
+
// Start the hash change handling, returning `true` if the current URL matches
|
1006
|
+
// an existing route, and `false` otherwise.
|
1007
|
+
start: function(options) {
|
1008
|
+
if (History.started) throw new Error("Backbone.history has already been started");
|
1009
|
+
History.started = true;
|
1010
|
+
|
1011
|
+
// Figure out the initial configuration. Do we need an iframe?
|
1012
|
+
// Is pushState desired ... is it available?
|
1013
|
+
this.options = _.extend({}, {root: '/'}, this.options, options);
|
1014
|
+
this._wantsHashChange = this.options.hashChange !== false;
|
1015
|
+
this._wantsPushState = !!this.options.pushState;
|
1016
|
+
this._hasPushState = !!(this.options.pushState && window.history && window.history.pushState);
|
1017
|
+
var fragment = this.getFragment();
|
1018
|
+
var docMode = document.documentMode;
|
1019
|
+
var oldIE = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7));
|
1020
|
+
|
1021
|
+
if (oldIE) {
|
1022
|
+
this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
|
1023
|
+
this.navigate(fragment);
|
1024
|
+
}
|
1025
|
+
|
1026
|
+
// Depending on whether we're using pushState or hashes, and whether
|
1027
|
+
// 'onhashchange' is supported, determine how we check the URL state.
|
1028
|
+
if (this._hasPushState) {
|
1029
|
+
$(window).bind('popstate', this.checkUrl);
|
1030
|
+
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
|
1031
|
+
$(window).bind('hashchange', this.checkUrl);
|
1032
|
+
} else if (this._wantsHashChange) {
|
1033
|
+
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
|
1034
|
+
}
|
1035
|
+
|
1036
|
+
// Determine if we need to change the base url, for a pushState link
|
1037
|
+
// opened by a non-pushState browser.
|
1038
|
+
this.fragment = fragment;
|
1039
|
+
var loc = window.location;
|
1040
|
+
var atRoot = loc.pathname == this.options.root;
|
1041
|
+
|
1042
|
+
// If we've started off with a route from a `pushState`-enabled browser,
|
1043
|
+
// but we're currently in a browser that doesn't support it...
|
1044
|
+
if (this._wantsHashChange && this._wantsPushState && !this._hasPushState && !atRoot) {
|
1045
|
+
this.fragment = this.getFragment(null, true);
|
1046
|
+
window.location.replace(this.options.root + '#' + this.fragment);
|
1047
|
+
// Return immediately as browser will do redirect to new url
|
1048
|
+
return true;
|
1049
|
+
|
1050
|
+
// Or if we've started out with a hash-based route, but we're currently
|
1051
|
+
// in a browser where it could be `pushState`-based instead...
|
1052
|
+
} else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
|
1053
|
+
this.fragment = this.getHash().replace(routeStripper, '');
|
1054
|
+
window.history.replaceState({}, document.title, loc.protocol + '//' + loc.host + this.options.root + this.fragment);
|
1055
|
+
}
|
1056
|
+
|
1057
|
+
if (!this.options.silent) {
|
1058
|
+
return this.loadUrl();
|
1059
|
+
}
|
1060
|
+
},
|
1061
|
+
|
1062
|
+
// Disable Backbone.history, perhaps temporarily. Not useful in a real app,
|
1063
|
+
// but possibly useful for unit testing Routers.
|
1064
|
+
stop: function() {
|
1065
|
+
$(window).unbind('popstate', this.checkUrl).unbind('hashchange', this.checkUrl);
|
1066
|
+
clearInterval(this._checkUrlInterval);
|
1067
|
+
History.started = false;
|
1068
|
+
},
|
1069
|
+
|
1070
|
+
// Add a route to be tested when the fragment changes. Routes added later
|
1071
|
+
// may override previous routes.
|
1072
|
+
route: function(route, callback) {
|
1073
|
+
this.handlers.unshift({route: route, callback: callback});
|
1074
|
+
},
|
1075
|
+
|
1076
|
+
// Checks the current URL to see if it has changed, and if it has,
|
1077
|
+
// calls `loadUrl`, normalizing across the hidden iframe.
|
1078
|
+
checkUrl: function(e) {
|
1079
|
+
var current = this.getFragment();
|
1080
|
+
if (current == this.fragment && this.iframe) current = this.getFragment(this.getHash(this.iframe));
|
1081
|
+
if (current == this.fragment) return false;
|
1082
|
+
if (this.iframe) this.navigate(current);
|
1083
|
+
this.loadUrl() || this.loadUrl(this.getHash());
|
1084
|
+
},
|
1085
|
+
|
1086
|
+
// Attempt to load the current URL fragment. If a route succeeds with a
|
1087
|
+
// match, returns `true`. If no defined routes matches the fragment,
|
1088
|
+
// returns `false`.
|
1089
|
+
loadUrl: function(fragmentOverride) {
|
1090
|
+
var fragment = this.fragment = this.getFragment(fragmentOverride);
|
1091
|
+
var matched = _.any(this.handlers, function(handler) {
|
1092
|
+
if (handler.route.test(fragment)) {
|
1093
|
+
handler.callback(fragment);
|
1094
|
+
return true;
|
1095
|
+
}
|
1096
|
+
});
|
1097
|
+
return matched;
|
1098
|
+
},
|
1099
|
+
|
1100
|
+
// Save a fragment into the hash history, or replace the URL state if the
|
1101
|
+
// 'replace' option is passed. You are responsible for properly URL-encoding
|
1102
|
+
// the fragment in advance.
|
1103
|
+
//
|
1104
|
+
// The options object can contain `trigger: true` if you wish to have the
|
1105
|
+
// route callback be fired (not usually desirable), or `replace: true`, if
|
1106
|
+
// you wish to modify the current URL without adding an entry to the history.
|
1107
|
+
navigate: function(fragment, options) {
|
1108
|
+
if (!History.started) return false;
|
1109
|
+
if (!options || options === true) options = {trigger: options};
|
1110
|
+
var frag = (fragment || '').replace(routeStripper, '');
|
1111
|
+
if (this.fragment == frag) return;
|
1112
|
+
|
1113
|
+
// If pushState is available, we use it to set the fragment as a real URL.
|
1114
|
+
if (this._hasPushState) {
|
1115
|
+
if (frag.indexOf(this.options.root) != 0) frag = this.options.root + frag;
|
1116
|
+
this.fragment = frag;
|
1117
|
+
window.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, frag);
|
1118
|
+
|
1119
|
+
// If hash changes haven't been explicitly disabled, update the hash
|
1120
|
+
// fragment to store history.
|
1121
|
+
} else if (this._wantsHashChange) {
|
1122
|
+
this.fragment = frag;
|
1123
|
+
this._updateHash(window.location, frag, options.replace);
|
1124
|
+
if (this.iframe && (frag != this.getFragment(this.getHash(this.iframe)))) {
|
1125
|
+
// Opening and closing the iframe tricks IE7 and earlier to push a history entry on hash-tag change.
|
1126
|
+
// When replace is true, we don't want this.
|
1127
|
+
if(!options.replace) this.iframe.document.open().close();
|
1128
|
+
this._updateHash(this.iframe.location, frag, options.replace);
|
1129
|
+
}
|
1130
|
+
|
1131
|
+
// If you've told us that you explicitly don't want fallback hashchange-
|
1132
|
+
// based history, then `navigate` becomes a page refresh.
|
1133
|
+
} else {
|
1134
|
+
window.location.assign(this.options.root + fragment);
|
1135
|
+
}
|
1136
|
+
if (options.trigger) this.loadUrl(fragment);
|
1137
|
+
},
|
1138
|
+
|
1139
|
+
// Update the hash location, either replacing the current entry, or adding
|
1140
|
+
// a new one to the browser history.
|
1141
|
+
_updateHash: function(location, fragment, replace) {
|
1142
|
+
if (replace) {
|
1143
|
+
location.replace(location.toString().replace(/(javascript:|#).*$/, '') + '#' + fragment);
|
1144
|
+
} else {
|
1145
|
+
location.hash = fragment;
|
1146
|
+
}
|
1147
|
+
}
|
1148
|
+
});
|
1149
|
+
|
1150
|
+
// Backbone.View
|
1151
|
+
// -------------
|
1152
|
+
|
1153
|
+
// Creating a Backbone.View creates its initial element outside of the DOM,
|
1154
|
+
// if an existing element is not provided...
|
1155
|
+
var View = Backbone.View = function(options) {
|
1156
|
+
this.cid = _.uniqueId('view');
|
1157
|
+
this._configure(options || {});
|
1158
|
+
this._ensureElement();
|
1159
|
+
this.initialize.apply(this, arguments);
|
1160
|
+
this.delegateEvents();
|
1161
|
+
};
|
1162
|
+
|
1163
|
+
// Cached regex to split keys for `delegate`.
|
1164
|
+
var delegateEventSplitter = /^(\S+)\s*(.*)$/;
|
1165
|
+
|
1166
|
+
// List of view options to be merged as properties.
|
1167
|
+
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];
|
1168
|
+
|
1169
|
+
// Set up all inheritable **Backbone.View** properties and methods.
|
1170
|
+
_.extend(View.prototype, Events, {
|
1171
|
+
|
1172
|
+
// The default `tagName` of a View's element is `"div"`.
|
1173
|
+
tagName: 'div',
|
1174
|
+
|
1175
|
+
// jQuery delegate for element lookup, scoped to DOM elements within the
|
1176
|
+
// current view. This should be prefered to global lookups where possible.
|
1177
|
+
$: function(selector) {
|
1178
|
+
return this.$el.find(selector);
|
1179
|
+
},
|
1180
|
+
|
1181
|
+
// Initialize is an empty function by default. Override it with your own
|
1182
|
+
// initialization logic.
|
1183
|
+
initialize: function(){},
|
1184
|
+
|
1185
|
+
// **render** is the core function that your view should override, in order
|
1186
|
+
// to populate its element (`this.el`), with the appropriate HTML. The
|
1187
|
+
// convention is for **render** to always return `this`.
|
1188
|
+
render: function() {
|
1189
|
+
return this;
|
1190
|
+
},
|
1191
|
+
|
1192
|
+
// Remove this view from the DOM. Note that the view isn't present in the
|
1193
|
+
// DOM by default, so calling this method may be a no-op.
|
1194
|
+
remove: function() {
|
1195
|
+
this.$el.remove();
|
1196
|
+
return this;
|
1197
|
+
},
|
1198
|
+
|
1199
|
+
// For small amounts of DOM Elements, where a full-blown template isn't
|
1200
|
+
// needed, use **make** to manufacture elements, one at a time.
|
1201
|
+
//
|
1202
|
+
// var el = this.make('li', {'class': 'row'}, this.model.escape('title'));
|
1203
|
+
//
|
1204
|
+
make: function(tagName, attributes, content) {
|
1205
|
+
var el = document.createElement(tagName);
|
1206
|
+
if (attributes) $(el).attr(attributes);
|
1207
|
+
if (content) $(el).html(content);
|
1208
|
+
return el;
|
1209
|
+
},
|
1210
|
+
|
1211
|
+
// Change the view's element (`this.el` property), including event
|
1212
|
+
// re-delegation.
|
1213
|
+
setElement: function(element, delegate) {
|
1214
|
+
if (this.$el) this.undelegateEvents();
|
1215
|
+
this.$el = (element instanceof $) ? element : $(element);
|
1216
|
+
this.el = this.$el[0];
|
1217
|
+
if (delegate !== false) this.delegateEvents();
|
1218
|
+
return this;
|
1219
|
+
},
|
1220
|
+
|
1221
|
+
// Set callbacks, where `this.events` is a hash of
|
1222
|
+
//
|
1223
|
+
// *{"event selector": "callback"}*
|
1224
|
+
//
|
1225
|
+
// {
|
1226
|
+
// 'mousedown .title': 'edit',
|
1227
|
+
// 'click .button': 'save'
|
1228
|
+
// 'click .open': function(e) { ... }
|
1229
|
+
// }
|
1230
|
+
//
|
1231
|
+
// pairs. Callbacks will be bound to the view, with `this` set properly.
|
1232
|
+
// Uses event delegation for efficiency.
|
1233
|
+
// Omitting the selector binds the event to `this.el`.
|
1234
|
+
// This only works for delegate-able events: not `focus`, `blur`, and
|
1235
|
+
// not `change`, `submit`, and `reset` in Internet Explorer.
|
1236
|
+
delegateEvents: function(events) {
|
1237
|
+
if (!(events || (events = getValue(this, 'events')))) return;
|
1238
|
+
this.undelegateEvents();
|
1239
|
+
for (var key in events) {
|
1240
|
+
var method = events[key];
|
1241
|
+
if (!_.isFunction(method)) method = this[events[key]];
|
1242
|
+
if (!method) throw new Error('Method "' + events[key] + '" does not exist');
|
1243
|
+
var match = key.match(delegateEventSplitter);
|
1244
|
+
var eventName = match[1], selector = match[2];
|
1245
|
+
method = _.bind(method, this);
|
1246
|
+
eventName += '.delegateEvents' + this.cid;
|
1247
|
+
if (selector === '') {
|
1248
|
+
this.$el.bind(eventName, method);
|
1249
|
+
} else {
|
1250
|
+
this.$el.delegate(selector, eventName, method);
|
1251
|
+
}
|
1252
|
+
}
|
1253
|
+
},
|
1254
|
+
|
1255
|
+
// Clears all callbacks previously bound to the view with `delegateEvents`.
|
1256
|
+
// You usually don't need to use this, but may wish to if you have multiple
|
1257
|
+
// Backbone views attached to the same DOM element.
|
1258
|
+
undelegateEvents: function() {
|
1259
|
+
this.$el.unbind('.delegateEvents' + this.cid);
|
1260
|
+
},
|
1261
|
+
|
1262
|
+
// Performs the initial configuration of a View with a set of options.
|
1263
|
+
// Keys with special meaning *(model, collection, id, className)*, are
|
1264
|
+
// attached directly to the view.
|
1265
|
+
_configure: function(options) {
|
1266
|
+
if (this.options) options = _.extend({}, this.options, options);
|
1267
|
+
for (var i = 0, l = viewOptions.length; i < l; i++) {
|
1268
|
+
var attr = viewOptions[i];
|
1269
|
+
if (options[attr]) this[attr] = options[attr];
|
1270
|
+
}
|
1271
|
+
this.options = options;
|
1272
|
+
},
|
1273
|
+
|
1274
|
+
// Ensure that the View has a DOM element to render into.
|
1275
|
+
// If `this.el` is a string, pass it through `$()`, take the first
|
1276
|
+
// matching element, and re-assign it to `el`. Otherwise, create
|
1277
|
+
// an element from the `id`, `className` and `tagName` properties.
|
1278
|
+
_ensureElement: function() {
|
1279
|
+
if (!this.el) {
|
1280
|
+
var attrs = getValue(this, 'attributes') || {};
|
1281
|
+
if (this.id) attrs.id = this.id;
|
1282
|
+
if (this.className) attrs['class'] = this.className;
|
1283
|
+
this.setElement(this.make(this.tagName, attrs), false);
|
1284
|
+
} else {
|
1285
|
+
this.setElement(this.el, false);
|
1286
|
+
}
|
1287
|
+
}
|
1288
|
+
|
1289
|
+
});
|
1290
|
+
|
1291
|
+
// The self-propagating extend function that Backbone classes use.
|
1292
|
+
var extend = function (protoProps, classProps) {
|
1293
|
+
var child = inherits(this, protoProps, classProps);
|
1294
|
+
child.extend = this.extend;
|
1295
|
+
return child;
|
1296
|
+
};
|
1297
|
+
|
1298
|
+
// Set up inheritance for the model, collection, and view.
|
1299
|
+
Model.extend = Collection.extend = Router.extend = View.extend = extend;
|
1300
|
+
|
1301
|
+
// Backbone.sync
|
1302
|
+
// -------------
|
1303
|
+
|
1304
|
+
// Map from CRUD to HTTP for our default `Backbone.sync` implementation.
|
1305
|
+
var methodMap = {
|
1306
|
+
'create': 'POST',
|
1307
|
+
'update': 'PUT',
|
1308
|
+
'delete': 'DELETE',
|
1309
|
+
'read': 'GET'
|
1310
|
+
};
|
1311
|
+
|
1312
|
+
// Override this function to change the manner in which Backbone persists
|
1313
|
+
// models to the server. You will be passed the type of request, and the
|
1314
|
+
// model in question. By default, makes a RESTful Ajax request
|
1315
|
+
// to the model's `url()`. Some possible customizations could be:
|
1316
|
+
//
|
1317
|
+
// * Use `setTimeout` to batch rapid-fire updates into a single request.
|
1318
|
+
// * Send up the models as XML instead of JSON.
|
1319
|
+
// * Persist models via WebSockets instead of Ajax.
|
1320
|
+
//
|
1321
|
+
// Turn on `Backbone.emulateHTTP` in order to send `PUT` and `DELETE` requests
|
1322
|
+
// as `POST`, with a `_method` parameter containing the true HTTP method,
|
1323
|
+
// as well as all requests with the body as `application/x-www-form-urlencoded`
|
1324
|
+
// instead of `application/json` with the model in a param named `model`.
|
1325
|
+
// Useful when interfacing with server-side languages like **PHP** that make
|
1326
|
+
// it difficult to read the body of `PUT` requests.
|
1327
|
+
Backbone.sync = function(method, model, options) {
|
1328
|
+
var type = methodMap[method];
|
1329
|
+
|
1330
|
+
// Default options, unless specified.
|
1331
|
+
options || (options = {});
|
1332
|
+
|
1333
|
+
// Default JSON-request options.
|
1334
|
+
var params = {type: type, dataType: 'json'};
|
1335
|
+
|
1336
|
+
// Ensure that we have a URL.
|
1337
|
+
if (!options.url) {
|
1338
|
+
params.url = getValue(model, 'url') || urlError();
|
1339
|
+
}
|
1340
|
+
|
1341
|
+
// Ensure that we have the appropriate request data.
|
1342
|
+
if (!options.data && model && (method == 'create' || method == 'update')) {
|
1343
|
+
params.contentType = 'application/json';
|
1344
|
+
params.data = JSON.stringify(model.toJSON());
|
1345
|
+
}
|
1346
|
+
|
1347
|
+
// For older servers, emulate JSON by encoding the request into an HTML-form.
|
1348
|
+
if (Backbone.emulateJSON) {
|
1349
|
+
params.contentType = 'application/x-www-form-urlencoded';
|
1350
|
+
params.data = params.data ? {model: params.data} : {};
|
1351
|
+
}
|
1352
|
+
|
1353
|
+
// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
|
1354
|
+
// And an `X-HTTP-Method-Override` header.
|
1355
|
+
if (Backbone.emulateHTTP) {
|
1356
|
+
if (type === 'PUT' || type === 'DELETE') {
|
1357
|
+
if (Backbone.emulateJSON) params.data._method = type;
|
1358
|
+
params.type = 'POST';
|
1359
|
+
params.beforeSend = function(xhr) {
|
1360
|
+
xhr.setRequestHeader('X-HTTP-Method-Override', type);
|
1361
|
+
};
|
1362
|
+
}
|
1363
|
+
}
|
1364
|
+
|
1365
|
+
// Don't process data on a non-GET request.
|
1366
|
+
if (params.type !== 'GET' && !Backbone.emulateJSON) {
|
1367
|
+
params.processData = false;
|
1368
|
+
}
|
1369
|
+
|
1370
|
+
// Make the request, allowing the user to override any Ajax options.
|
1371
|
+
return $.ajax(_.extend(params, options));
|
1372
|
+
};
|
1373
|
+
|
1374
|
+
// Wrap an optional error callback with a fallback error event.
|
1375
|
+
Backbone.wrapError = function(onError, originalModel, options) {
|
1376
|
+
return function(model, resp) {
|
1377
|
+
resp = model === originalModel ? resp : model;
|
1378
|
+
if (onError) {
|
1379
|
+
onError(originalModel, resp, options);
|
1380
|
+
} else {
|
1381
|
+
originalModel.trigger('error', originalModel, resp, options);
|
1382
|
+
}
|
1383
|
+
};
|
1384
|
+
};
|
1385
|
+
|
1386
|
+
// Helpers
|
1387
|
+
// -------
|
1388
|
+
|
1389
|
+
// Shared empty constructor function to aid in prototype-chain creation.
|
1390
|
+
var ctor = function(){};
|
1391
|
+
|
1392
|
+
// Helper function to correctly set up the prototype chain, for subclasses.
|
1393
|
+
// Similar to `goog.inherits`, but uses a hash of prototype properties and
|
1394
|
+
// class properties to be extended.
|
1395
|
+
var inherits = function(parent, protoProps, staticProps) {
|
1396
|
+
var child;
|
1397
|
+
|
1398
|
+
// The constructor function for the new subclass is either defined by you
|
1399
|
+
// (the "constructor" property in your `extend` definition), or defaulted
|
1400
|
+
// by us to simply call the parent's constructor.
|
1401
|
+
if (protoProps && protoProps.hasOwnProperty('constructor')) {
|
1402
|
+
child = protoProps.constructor;
|
1403
|
+
} else {
|
1404
|
+
child = function(){ parent.apply(this, arguments); };
|
1405
|
+
}
|
1406
|
+
|
1407
|
+
// Inherit class (static) properties from parent.
|
1408
|
+
_.extend(child, parent);
|
1409
|
+
|
1410
|
+
// Set the prototype chain to inherit from `parent`, without calling
|
1411
|
+
// `parent`'s constructor function.
|
1412
|
+
ctor.prototype = parent.prototype;
|
1413
|
+
child.prototype = new ctor();
|
1414
|
+
|
1415
|
+
// Add prototype properties (instance properties) to the subclass,
|
1416
|
+
// if supplied.
|
1417
|
+
if (protoProps) _.extend(child.prototype, protoProps);
|
1418
|
+
|
1419
|
+
// Add static properties to the constructor function, if supplied.
|
1420
|
+
if (staticProps) _.extend(child, staticProps);
|
1421
|
+
|
1422
|
+
// Correctly set child's `prototype.constructor`.
|
1423
|
+
child.prototype.constructor = child;
|
1424
|
+
|
1425
|
+
// Set a convenience property in case the parent's prototype is needed later.
|
1426
|
+
child.__super__ = parent.prototype;
|
1427
|
+
|
1428
|
+
return child;
|
1429
|
+
};
|
1430
|
+
|
1431
|
+
// Helper function to get a value from a Backbone object as a property
|
1432
|
+
// or as a function.
|
1433
|
+
var getValue = function(object, prop) {
|
1434
|
+
if (!(object && object[prop])) return null;
|
1435
|
+
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
|
1436
|
+
};
|
1437
|
+
|
1438
|
+
// Throw an error when a URL is needed, and none is supplied.
|
1439
|
+
var urlError = function() {
|
1440
|
+
throw new Error('A "url" property or function must be specified');
|
1441
|
+
};
|
1442
|
+
|
1443
|
+
}).call(this);
|