backbone-handlebars 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.document +5 -0
  2. data/Gemfile +16 -0
  3. data/Guardfile +18 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.markdown +58 -0
  6. data/Rakefile +67 -0
  7. data/VERSION +1 -0
  8. data/generators/backbone_view/backbone_view_generator.rb +9 -0
  9. data/generators/backbone_view/templates/view.coffee.erb +2 -0
  10. data/generators/backbone_view/templates/view_template.html.handlebars +2 -0
  11. data/lib/assets/javascripts/handlebars_helpers.coffee +46 -0
  12. data/lib/assets/javascripts/handlebars_view.coffee +26 -0
  13. data/lib/backbone-handlebars.rb +5 -0
  14. data/lib/backbone-handlebars/engine.rb +9 -0
  15. data/lib/backbone-handlebars/helper.rb +13 -0
  16. data/lib/generators/backbone/USAGE +8 -0
  17. data/lib/generators/backbone/backbone_generator.rb +6 -0
  18. data/rakelib/coffeescript.rake +30 -0
  19. data/spec/javascripts/fixtures/child_view.handlebars.html +4 -0
  20. data/spec/javascripts/fixtures/multiple_child_parent_view.handlebars.html +7 -0
  21. data/spec/javascripts/fixtures/parent_view.handlebars.html +6 -0
  22. data/spec/javascripts/fixtures/simple_view.handlebars.html +4 -0
  23. data/spec/javascripts/handlebars_view_spec.coffee +55 -0
  24. data/spec/javascripts/helpers/SpecHelper.js +9 -0
  25. data/spec/javascripts/helpers/loadTemplateHelper.js +17 -0
  26. data/spec/javascripts/jasmine-jquery-1.1.3.js +205 -0
  27. data/spec/javascripts/jquery.js +167 -0
  28. data/spec/javascripts/support/jasmine.yml +78 -0
  29. data/spec/javascripts/support/jasmine_config.rb +23 -0
  30. data/spec/javascripts/support/jasmine_runner.rb +32 -0
  31. data/test/helper.rb +18 -0
  32. data/test/test_backbone-handlebars.rb +7 -0
  33. data/vendor/assets/javascripts/backbone.js +1066 -0
  34. data/vendor/assets/javascripts/handlebars.js +1417 -0
  35. data/vendor/assets/javascripts/underscore.js +748 -0
  36. metadata +164 -0
@@ -0,0 +1,23 @@
1
+ module Jasmine
2
+ class Config
3
+
4
+ # Add your overrides or custom config code here
5
+
6
+ end
7
+ end
8
+
9
+
10
+ # Note - this is necessary for rspec2, which has removed the backtrace
11
+ module Jasmine
12
+ class SpecBuilder
13
+ def declare_spec(parent, spec)
14
+ me = self
15
+ example_name = spec["name"]
16
+ @spec_ids << spec["id"]
17
+ backtrace = @example_locations[parent.description + " " + example_name]
18
+ parent.it example_name, {} do
19
+ me.report_spec(spec["id"])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ $:.unshift(ENV['JASMINE_GEM_PATH']) if ENV['JASMINE_GEM_PATH'] # for gem testing purposes
2
+
3
+ require 'rubygems'
4
+ require 'jasmine'
5
+ jasmine_config_overrides = File.expand_path(File.join(File.dirname(__FILE__), 'jasmine_config.rb'))
6
+ require jasmine_config_overrides if File.exist?(jasmine_config_overrides)
7
+ if Jasmine::rspec2?
8
+ require 'rspec'
9
+ else
10
+ require 'spec'
11
+ end
12
+
13
+ jasmine_config = Jasmine::Config.new
14
+ spec_builder = Jasmine::SpecBuilder.new(jasmine_config)
15
+
16
+ should_stop = false
17
+
18
+ if Jasmine::rspec2?
19
+ RSpec.configuration.after(:suite) do
20
+ spec_builder.stop if should_stop
21
+ end
22
+ else
23
+ Spec::Runner.configure do |config|
24
+ config.after(:suite) do
25
+ spec_builder.stop if should_stop
26
+ end
27
+ end
28
+ end
29
+
30
+ spec_builder.start
31
+ should_stop = true
32
+ spec_builder.declare_suites
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'backbone-handlebars'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestBackboneHandlebars < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,1066 @@
1
+ // Backbone.js 0.3.3
2
+ // (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
3
+ // Backbone may be freely distributed under the MIT license.
4
+ // For all details and documentation:
5
+ // http://documentcloud.github.com/backbone
6
+
7
+ (function(){
8
+
9
+ // Initial Setup
10
+ // -------------
11
+
12
+ // The top-level namespace. All public Backbone classes and modules will
13
+ // be attached to this. Exported for both CommonJS and the browser.
14
+ var Backbone;
15
+ if (typeof exports !== 'undefined') {
16
+ Backbone = exports;
17
+ } else {
18
+ Backbone = this.Backbone = {};
19
+ }
20
+
21
+ // Current version of the library. Keep in sync with `package.json`.
22
+ Backbone.VERSION = '0.3.3';
23
+
24
+ // Require Underscore, if we're on the server, and it's not already present.
25
+ var _ = this._;
26
+ if (!_ && (typeof require !== 'undefined')) _ = require('underscore')._;
27
+
28
+ // For Backbone's purposes, either jQuery or Zepto owns the `$` variable.
29
+ var $ = this.jQuery || this.Zepto;
30
+
31
+ // Turn on `emulateHTTP` to use support legacy HTTP servers. Setting this option will
32
+ // fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a
33
+ // `X-Http-Method-Override` header.
34
+ Backbone.emulateHTTP = false;
35
+
36
+ // Turn on `emulateJSON` to support legacy servers that can't deal with direct
37
+ // `application/json` requests ... will encode the body as
38
+ // `application/x-www-form-urlencoded` instead and will send the model in a
39
+ // form param named `model`.
40
+ Backbone.emulateJSON = false;
41
+
42
+ // Backbone.Events
43
+ // -----------------
44
+
45
+ // A module that can be mixed in to *any object* in order to provide it with
46
+ // custom events. You may `bind` or `unbind` a callback function to an event;
47
+ // `trigger`-ing an event fires all callbacks in succession.
48
+ //
49
+ // var object = {};
50
+ // _.extend(object, Backbone.Events);
51
+ // object.bind('expand', function(){ alert('expanded'); });
52
+ // object.trigger('expand');
53
+ //
54
+ Backbone.Events = {
55
+
56
+ // Bind an event, specified by a string name, `ev`, to a `callback` function.
57
+ // Passing `"all"` will bind the callback to all events fired.
58
+ bind : function(ev, callback) {
59
+ var calls = this._callbacks || (this._callbacks = {});
60
+ var list = this._callbacks[ev] || (this._callbacks[ev] = []);
61
+ list.push(callback);
62
+ return this;
63
+ },
64
+
65
+ // Remove one or many callbacks. If `callback` is null, removes all
66
+ // callbacks for the event. If `ev` is null, removes all bound callbacks
67
+ // for all events.
68
+ unbind : function(ev, callback) {
69
+ var calls;
70
+ if (!ev) {
71
+ this._callbacks = {};
72
+ } else if (calls = this._callbacks) {
73
+ if (!callback) {
74
+ calls[ev] = [];
75
+ } else {
76
+ var list = calls[ev];
77
+ if (!list) return this;
78
+ for (var i = 0, l = list.length; i < l; i++) {
79
+ if (callback === list[i]) {
80
+ list.splice(i, 1);
81
+ break;
82
+ }
83
+ }
84
+ }
85
+ }
86
+ return this;
87
+ },
88
+
89
+ // Trigger an event, firing all bound callbacks. Callbacks are passed the
90
+ // same arguments as `trigger` is, apart from the event name.
91
+ // Listening for `"all"` passes the true event name as the first argument.
92
+ trigger : function(ev) {
93
+ var list, calls, i, l;
94
+ if (!(calls = this._callbacks)) return this;
95
+ if (calls[ev]) {
96
+ list = calls[ev].slice(0);
97
+ for (i = 0, l = list.length; i < l; i++) {
98
+ list[i].apply(this, Array.prototype.slice.call(arguments, 1));
99
+ }
100
+ }
101
+ if (calls['all']) {
102
+ list = calls['all'].slice(0);
103
+ for (i = 0, l = list.length; i < l; i++) {
104
+ list[i].apply(this, arguments);
105
+ }
106
+ }
107
+ return this;
108
+ }
109
+
110
+ };
111
+
112
+ // Backbone.Model
113
+ // --------------
114
+
115
+ // Create a new model, with defined attributes. A client id (`cid`)
116
+ // is automatically generated and assigned for you.
117
+ Backbone.Model = function(attributes, options) {
118
+ var defaults;
119
+ attributes || (attributes = {});
120
+ if (defaults = this.defaults) {
121
+ if (_.isFunction(defaults)) defaults = defaults();
122
+ attributes = _.extend({}, defaults, attributes);
123
+ }
124
+ this.attributes = {};
125
+ this._escapedAttributes = {};
126
+ this.cid = _.uniqueId('c');
127
+ this.set(attributes, {silent : true});
128
+ this._changed = false;
129
+ this._previousAttributes = _.clone(this.attributes);
130
+ if (options && options.collection) this.collection = options.collection;
131
+ this.initialize(attributes, options);
132
+ };
133
+
134
+ // Attach all inheritable methods to the Model prototype.
135
+ _.extend(Backbone.Model.prototype, Backbone.Events, {
136
+
137
+ // A snapshot of the model's previous attributes, taken immediately
138
+ // after the last `"change"` event was fired.
139
+ _previousAttributes : null,
140
+
141
+ // Has the item been changed since the last `"change"` event?
142
+ _changed : false,
143
+
144
+ // Initialize is an empty function by default. Override it with your own
145
+ // initialization logic.
146
+ initialize : function(){},
147
+
148
+ // Return a copy of the model's `attributes` object.
149
+ toJSON : function() {
150
+ return _.clone(this.attributes);
151
+ },
152
+
153
+ // Get the value of an attribute.
154
+ get : function(attr) {
155
+ return this.attributes[attr];
156
+ },
157
+
158
+ // Get the HTML-escaped value of an attribute.
159
+ escape : function(attr) {
160
+ var html;
161
+ if (html = this._escapedAttributes[attr]) return html;
162
+ var val = this.attributes[attr];
163
+ return this._escapedAttributes[attr] = escapeHTML(val == null ? '' : '' + val);
164
+ },
165
+
166
+ // Returns `true` if the attribute contains a value that is not null
167
+ // or undefined.
168
+ has : function(attr) {
169
+ return this.attributes[attr] != null;
170
+ },
171
+
172
+ // Set a hash of model attributes on the object, firing `"change"` unless you
173
+ // choose to silence it.
174
+ set : function(attrs, options) {
175
+
176
+ // Extract attributes and options.
177
+ options || (options = {});
178
+ if (!attrs) return this;
179
+ if (attrs.attributes) attrs = attrs.attributes;
180
+ var now = this.attributes, escaped = this._escapedAttributes;
181
+
182
+ // Run validation.
183
+ if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;
184
+
185
+ // Check for changes of `id`.
186
+ if ('id' in attrs) this.id = attrs.id;
187
+
188
+ // Update attributes.
189
+ for (var attr in attrs) {
190
+ var val = attrs[attr];
191
+ if (!_.isEqual(now[attr], val)) {
192
+ now[attr] = val;
193
+ delete escaped[attr];
194
+ this._changed = true;
195
+ if (!options.silent) this.trigger('change:' + attr, this, val, options);
196
+ }
197
+ }
198
+
199
+ // Fire the `"change"` event, if the model has been changed.
200
+ if (!options.silent && this._changed) this.change(options);
201
+ return this;
202
+ },
203
+
204
+ // Remove an attribute from the model, firing `"change"` unless you choose
205
+ // to silence it.
206
+ unset : function(attr, options) {
207
+ options || (options = {});
208
+ var value = this.attributes[attr];
209
+
210
+ // Run validation.
211
+ var validObj = {};
212
+ validObj[attr] = void 0;
213
+ if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
214
+
215
+ // Remove the attribute.
216
+ delete this.attributes[attr];
217
+ delete this._escapedAttributes[attr];
218
+ this._changed = true;
219
+ if (!options.silent) {
220
+ this.trigger('change:' + attr, this, void 0, options);
221
+ this.change(options);
222
+ }
223
+ return this;
224
+ },
225
+
226
+ // Clear all attributes on the model, firing `"change"` unless you choose
227
+ // to silence it.
228
+ clear : function(options) {
229
+ options || (options = {});
230
+ var old = this.attributes;
231
+
232
+ // Run validation.
233
+ var validObj = {};
234
+ for (attr in old) validObj[attr] = void 0;
235
+ if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
236
+
237
+ this.attributes = {};
238
+ this._escapedAttributes = {};
239
+ this._changed = true;
240
+ if (!options.silent) {
241
+ for (attr in old) {
242
+ this.trigger('change:' + attr, this, void 0, options);
243
+ }
244
+ this.change(options);
245
+ }
246
+ return this;
247
+ },
248
+
249
+ // Fetch the model from the server. If the server's representation of the
250
+ // model differs from its current attributes, they will be overriden,
251
+ // triggering a `"change"` event.
252
+ fetch : function(options) {
253
+ options || (options = {});
254
+ var model = this;
255
+ var success = options.success;
256
+ options.success = function(resp) {
257
+ if (!model.set(model.parse(resp), options)) return false;
258
+ if (success) success(model, resp);
259
+ };
260
+ options.error = wrapError(options.error, model, options);
261
+ (this.sync || Backbone.sync)('read', this, options);
262
+ return this;
263
+ },
264
+
265
+ // Set a hash of model attributes, and sync the model to the server.
266
+ // If the server returns an attributes hash that differs, the model's
267
+ // state will be `set` again.
268
+ save : function(attrs, options) {
269
+ options || (options = {});
270
+ if (attrs && !this.set(attrs, options)) return false;
271
+ var model = this;
272
+ var success = options.success;
273
+ options.success = function(resp) {
274
+ if (!model.set(model.parse(resp), options)) return false;
275
+ if (success) success(model, resp);
276
+ };
277
+ options.error = wrapError(options.error, model, options);
278
+ var method = this.isNew() ? 'create' : 'update';
279
+ (this.sync || Backbone.sync)(method, this, options);
280
+ return this;
281
+ },
282
+
283
+ // Destroy this model on the server. Upon success, the model is removed
284
+ // from its collection, if it has one.
285
+ destroy : function(options) {
286
+ options || (options = {});
287
+ var model = this;
288
+ var success = options.success;
289
+ options.success = function(resp) {
290
+ model.trigger('destroy', model, model.collection, options);
291
+ if (success) success(model, resp);
292
+ };
293
+ options.error = wrapError(options.error, model, options);
294
+ (this.sync || Backbone.sync)('delete', this, options);
295
+ return this;
296
+ },
297
+
298
+ // Default URL for the model's representation on the server -- if you're
299
+ // using Backbone's restful methods, override this to change the endpoint
300
+ // that will be called.
301
+ url : function() {
302
+ var base = getUrl(this.collection) || this.urlRoot || urlError();
303
+ if (this.isNew()) return base;
304
+ return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
305
+ },
306
+
307
+ // **parse** converts a response into the hash of attributes to be `set` on
308
+ // the model. The default implementation is just to pass the response along.
309
+ parse : function(resp) {
310
+ return resp;
311
+ },
312
+
313
+ // Create a new model with identical attributes to this one.
314
+ clone : function() {
315
+ return new this.constructor(this);
316
+ },
317
+
318
+ // A model is new if it has never been saved to the server, and has a negative
319
+ // ID.
320
+ isNew : function() {
321
+ return !this.id;
322
+ },
323
+
324
+ // Call this method to manually fire a `change` event for this model.
325
+ // Calling this will cause all objects observing the model to update.
326
+ change : function(options) {
327
+ this.trigger('change', this, options);
328
+ this._previousAttributes = _.clone(this.attributes);
329
+ this._changed = false;
330
+ },
331
+
332
+ // Determine if the model has changed since the last `"change"` event.
333
+ // If you specify an attribute name, determine if that attribute has changed.
334
+ hasChanged : function(attr) {
335
+ if (attr) return this._previousAttributes[attr] != this.attributes[attr];
336
+ return this._changed;
337
+ },
338
+
339
+ // Return an object containing all the attributes that have changed, or false
340
+ // if there are no changed attributes. Useful for determining what parts of a
341
+ // view need to be updated and/or what attributes need to be persisted to
342
+ // the server.
343
+ changedAttributes : function(now) {
344
+ now || (now = this.attributes);
345
+ var old = this._previousAttributes;
346
+ var changed = false;
347
+ for (var attr in now) {
348
+ if (!_.isEqual(old[attr], now[attr])) {
349
+ changed = changed || {};
350
+ changed[attr] = now[attr];
351
+ }
352
+ }
353
+ return changed;
354
+ },
355
+
356
+ // Get the previous value of an attribute, recorded at the time the last
357
+ // `"change"` event was fired.
358
+ previous : function(attr) {
359
+ if (!attr || !this._previousAttributes) return null;
360
+ return this._previousAttributes[attr];
361
+ },
362
+
363
+ // Get all of the attributes of the model at the time of the previous
364
+ // `"change"` event.
365
+ previousAttributes : function() {
366
+ return _.clone(this._previousAttributes);
367
+ },
368
+
369
+ // Run validation against a set of incoming attributes, returning `true`
370
+ // if all is well. If a specific `error` callback has been passed,
371
+ // call that instead of firing the general `"error"` event.
372
+ _performValidation : function(attrs, options) {
373
+ var error = this.validate(attrs);
374
+ if (error) {
375
+ if (options.error) {
376
+ options.error(this, error);
377
+ } else {
378
+ this.trigger('error', this, error, options);
379
+ }
380
+ return false;
381
+ }
382
+ return true;
383
+ }
384
+
385
+ });
386
+
387
+ // Backbone.Collection
388
+ // -------------------
389
+
390
+ // Provides a standard collection class for our sets of models, ordered
391
+ // or unordered. If a `comparator` is specified, the Collection will maintain
392
+ // its models in sort order, as they're added and removed.
393
+ Backbone.Collection = function(models, options) {
394
+ options || (options = {});
395
+ if (options.comparator) {
396
+ this.comparator = options.comparator;
397
+ delete options.comparator;
398
+ }
399
+ _.bindAll(this, '_onModelEvent', '_removeReference');
400
+ this._reset();
401
+ if (models) this.refresh(models, {silent: true});
402
+ this.initialize(models, options);
403
+ };
404
+
405
+ // Define the Collection's inheritable methods.
406
+ _.extend(Backbone.Collection.prototype, Backbone.Events, {
407
+
408
+ // The default model for a collection is just a **Backbone.Model**.
409
+ // This should be overridden in most cases.
410
+ model : Backbone.Model,
411
+
412
+ // Initialize is an empty function by default. Override it with your own
413
+ // initialization logic.
414
+ initialize : function(){},
415
+
416
+ // The JSON representation of a Collection is an array of the
417
+ // models' attributes.
418
+ toJSON : function() {
419
+ return this.map(function(model){ return model.toJSON(); });
420
+ },
421
+
422
+ // Add a model, or list of models to the set. Pass **silent** to avoid
423
+ // firing the `added` event for every new model.
424
+ add : function(models, options) {
425
+ if (_.isArray(models)) {
426
+ for (var i = 0, l = models.length; i < l; i++) {
427
+ this._add(models[i], options);
428
+ }
429
+ } else {
430
+ this._add(models, options);
431
+ }
432
+ return this;
433
+ },
434
+
435
+ // Remove a model, or a list of models from the set. Pass silent to avoid
436
+ // firing the `removed` event for every model removed.
437
+ remove : function(models, options) {
438
+ if (_.isArray(models)) {
439
+ for (var i = 0, l = models.length; i < l; i++) {
440
+ this._remove(models[i], options);
441
+ }
442
+ } else {
443
+ this._remove(models, options);
444
+ }
445
+ return this;
446
+ },
447
+
448
+ // Get a model from the set by id.
449
+ get : function(id) {
450
+ if (id == null) return null;
451
+ return this._byId[id.id != null ? id.id : id];
452
+ },
453
+
454
+ // Get a model from the set by client id.
455
+ getByCid : function(cid) {
456
+ return cid && this._byCid[cid.cid || cid];
457
+ },
458
+
459
+ // Get the model at the given index.
460
+ at: function(index) {
461
+ return this.models[index];
462
+ },
463
+
464
+ // Force the collection to re-sort itself. You don't need to call this under normal
465
+ // circumstances, as the set will maintain sort order as each item is added.
466
+ sort : function(options) {
467
+ options || (options = {});
468
+ if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
469
+ this.models = this.sortBy(this.comparator);
470
+ if (!options.silent) this.trigger('refresh', this, options);
471
+ return this;
472
+ },
473
+
474
+ // Pluck an attribute from each model in the collection.
475
+ pluck : function(attr) {
476
+ return _.map(this.models, function(model){ return model.get(attr); });
477
+ },
478
+
479
+ // When you have more items than you want to add or remove individually,
480
+ // you can refresh the entire set with a new list of models, without firing
481
+ // any `added` or `removed` events. Fires `refresh` when finished.
482
+ refresh : function(models, options) {
483
+ models || (models = []);
484
+ options || (options = {});
485
+ this.each(this._removeReference);
486
+ this._reset();
487
+ this.add(models, {silent: true});
488
+ if (!options.silent) this.trigger('refresh', this, options);
489
+ return this;
490
+ },
491
+
492
+ // Fetch the default set of models for this collection, refreshing the
493
+ // collection when they arrive. If `add: true` is passed, appends the
494
+ // models to the collection instead of refreshing.
495
+ fetch : function(options) {
496
+ options || (options = {});
497
+ var collection = this;
498
+ var success = options.success;
499
+ options.success = function(resp) {
500
+ collection[options.add ? 'add' : 'refresh'](collection.parse(resp), options);
501
+ if (success) success(collection, resp);
502
+ };
503
+ options.error = wrapError(options.error, collection, options);
504
+ (this.sync || Backbone.sync)('read', this, options);
505
+ return this;
506
+ },
507
+
508
+ // Create a new instance of a model in this collection. After the model
509
+ // has been created on the server, it will be added to the collection.
510
+ create : function(model, options) {
511
+ var coll = this;
512
+ options || (options = {});
513
+ if (!(model instanceof Backbone.Model)) {
514
+ model = new this.model(model, {collection: coll});
515
+ } else {
516
+ model.collection = coll;
517
+ }
518
+ var success = options.success;
519
+ options.success = function(nextModel, resp) {
520
+ coll.add(nextModel);
521
+ if (success) success(nextModel, resp);
522
+ };
523
+ return model.save(null, options);
524
+ },
525
+
526
+ // **parse** converts a response into a list of models to be added to the
527
+ // collection. The default implementation is just to pass it through.
528
+ parse : function(resp) {
529
+ return resp;
530
+ },
531
+
532
+ // Proxy to _'s chain. Can't be proxied the same way the rest of the
533
+ // underscore methods are proxied because it relies on the underscore
534
+ // constructor.
535
+ chain: function () {
536
+ return _(this.models).chain();
537
+ },
538
+
539
+ // Reset all internal state. Called when the collection is refreshed.
540
+ _reset : function(options) {
541
+ this.length = 0;
542
+ this.models = [];
543
+ this._byId = {};
544
+ this._byCid = {};
545
+ },
546
+
547
+ // Internal implementation of adding a single model to the set, updating
548
+ // hash indexes for `id` and `cid` lookups.
549
+ _add : function(model, options) {
550
+ options || (options = {});
551
+ if (!(model instanceof Backbone.Model)) {
552
+ model = new this.model(model, {collection: this});
553
+ }
554
+ var already = this.getByCid(model);
555
+ if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
556
+ this._byId[model.id] = model;
557
+ this._byCid[model.cid] = model;
558
+ if (!model.collection) {
559
+ model.collection = this;
560
+ }
561
+ var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length;
562
+ this.models.splice(index, 0, model);
563
+ model.bind('all', this._onModelEvent);
564
+ this.length++;
565
+ if (!options.silent) model.trigger('add', model, this, options);
566
+ return model;
567
+ },
568
+
569
+ // Internal implementation of removing a single model from the set, updating
570
+ // hash indexes for `id` and `cid` lookups.
571
+ _remove : function(model, options) {
572
+ options || (options = {});
573
+ model = this.getByCid(model) || this.get(model);
574
+ if (!model) return null;
575
+ delete this._byId[model.id];
576
+ delete this._byCid[model.cid];
577
+ this.models.splice(this.indexOf(model), 1);
578
+ this.length--;
579
+ if (!options.silent) model.trigger('remove', model, this, options);
580
+ this._removeReference(model);
581
+ return model;
582
+ },
583
+
584
+ // Internal method to remove a model's ties to a collection.
585
+ _removeReference : function(model) {
586
+ if (this == model.collection) {
587
+ delete model.collection;
588
+ }
589
+ model.unbind('all', this._onModelEvent);
590
+ },
591
+
592
+ // Internal method called every time a model in the set fires an event.
593
+ // Sets need to update their indexes when models change ids. All other
594
+ // events simply proxy through. "add" and "remove" events that originate
595
+ // in other collections are ignored.
596
+ _onModelEvent : function(ev, model, collection, options) {
597
+ if ((ev == 'add' || ev == 'remove') && collection != this) return;
598
+ if (ev == 'destroy') {
599
+ this._remove(model, options);
600
+ }
601
+ if (ev === 'change:id') {
602
+ delete this._byId[model.previous('id')];
603
+ this._byId[model.id] = model;
604
+ }
605
+ this.trigger.apply(this, arguments);
606
+ }
607
+
608
+ });
609
+
610
+ // Underscore methods that we want to implement on the Collection.
611
+ var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find', 'detect',
612
+ 'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 'include',
613
+ 'invoke', 'max', 'min', 'sortBy', 'sortedIndex', 'toArray', 'size',
614
+ 'first', 'rest', 'last', 'without', 'indexOf', 'lastIndexOf', 'isEmpty'];
615
+
616
+ // Mix in each Underscore method as a proxy to `Collection#models`.
617
+ _.each(methods, function(method) {
618
+ Backbone.Collection.prototype[method] = function() {
619
+ return _[method].apply(_, [this.models].concat(_.toArray(arguments)));
620
+ };
621
+ });
622
+
623
+ // Backbone.Controller
624
+ // -------------------
625
+
626
+ // Controllers map faux-URLs to actions, and fire events when routes are
627
+ // matched. Creating a new one sets its `routes` hash, if not set statically.
628
+ Backbone.Controller = function(options) {
629
+ options || (options = {});
630
+ if (options.routes) this.routes = options.routes;
631
+ this._bindRoutes();
632
+ this.initialize(options);
633
+ };
634
+
635
+ // Cached regular expressions for matching named param parts and splatted
636
+ // parts of route strings.
637
+ var namedParam = /:([\w\d]+)/g;
638
+ var splatParam = /\*([\w\d]+)/g;
639
+ var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
640
+
641
+ // Set up all inheritable **Backbone.Controller** properties and methods.
642
+ _.extend(Backbone.Controller.prototype, Backbone.Events, {
643
+
644
+ // Initialize is an empty function by default. Override it with your own
645
+ // initialization logic.
646
+ initialize : function(){},
647
+
648
+ // Manually bind a single named route to a callback. For example:
649
+ //
650
+ // this.route('search/:query/p:num', 'search', function(query, num) {
651
+ // ...
652
+ // });
653
+ //
654
+ route : function(route, name, callback) {
655
+ Backbone.history || (Backbone.history = new Backbone.History);
656
+ if (!_.isRegExp(route)) route = this._routeToRegExp(route);
657
+ Backbone.history.route(route, _.bind(function(fragment) {
658
+ var args = this._extractParameters(route, fragment);
659
+ callback.apply(this, args);
660
+ this.trigger.apply(this, ['route:' + name].concat(args));
661
+ }, this));
662
+ },
663
+
664
+ // Simple proxy to `Backbone.history` to save a fragment into the history,
665
+ // without triggering routes.
666
+ saveLocation : function(fragment) {
667
+ Backbone.history.saveLocation(fragment);
668
+ },
669
+
670
+ // Bind all defined routes to `Backbone.history`.
671
+ _bindRoutes : function() {
672
+ if (!this.routes) return;
673
+ for (var route in this.routes) {
674
+ var name = this.routes[route];
675
+ this.route(route, name, this[name]);
676
+ }
677
+ },
678
+
679
+ // Convert a route string into a regular expression, suitable for matching
680
+ // against the current location fragment.
681
+ _routeToRegExp : function(route) {
682
+ route = route.replace(escapeRegExp, "\\$&")
683
+ .replace(namedParam, "([^\/]*)")
684
+ .replace(splatParam, "(.*?)");
685
+ return new RegExp('^' + route + '$');
686
+ },
687
+
688
+ // Given a route, and a URL fragment that it matches, return the array of
689
+ // extracted parameters.
690
+ _extractParameters : function(route, fragment) {
691
+ return route.exec(fragment).slice(1);
692
+ }
693
+
694
+ });
695
+
696
+ // Backbone.History
697
+ // ----------------
698
+
699
+ // Handles cross-browser history management, based on URL hashes. If the
700
+ // browser does not support `onhashchange`, falls back to polling.
701
+ Backbone.History = function() {
702
+ this.handlers = [];
703
+ this.fragment = this.getFragment();
704
+ _.bindAll(this, 'checkUrl');
705
+ };
706
+
707
+ // Cached regex for cleaning hashes.
708
+ var hashStrip = /^#*/;
709
+
710
+ // Set up all inheritable **Backbone.History** properties and methods.
711
+ _.extend(Backbone.History.prototype, {
712
+
713
+ // The default interval to poll for hash changes, if necessary, is
714
+ // twenty times a second.
715
+ interval: 50,
716
+
717
+ // Get the cross-browser normalized URL fragment.
718
+ getFragment : function(loc) {
719
+ return (loc || window.location).hash.replace(hashStrip, '');
720
+ },
721
+
722
+ // Start the hash change handling, returning `true` if the current URL matches
723
+ // an existing route, and `false` otherwise.
724
+ start : function() {
725
+ var docMode = document.documentMode;
726
+ var oldIE = ($.browser.msie && (!docMode || docMode <= 7));
727
+ if (oldIE) {
728
+ this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
729
+ }
730
+ if ('onhashchange' in window && !oldIE) {
731
+ $(window).bind('hashchange', this.checkUrl);
732
+ } else {
733
+ setInterval(this.checkUrl, this.interval);
734
+ }
735
+ return this.loadUrl();
736
+ },
737
+
738
+ // Add a route to be tested when the hash changes. Routes are matched in the
739
+ // order they are added.
740
+ route : function(route, callback) {
741
+ this.handlers.push({route : route, callback : callback});
742
+ },
743
+
744
+ // Checks the current URL to see if it has changed, and if it has,
745
+ // calls `loadUrl`, normalizing across the hidden iframe.
746
+ checkUrl : function() {
747
+ var current = this.getFragment();
748
+ if (current == this.fragment && this.iframe) {
749
+ current = this.getFragment(this.iframe.location);
750
+ }
751
+ if (current == this.fragment ||
752
+ current == decodeURIComponent(this.fragment)) return false;
753
+ if (this.iframe) {
754
+ window.location.hash = this.iframe.location.hash = current;
755
+ }
756
+ this.loadUrl();
757
+ },
758
+
759
+ // Attempt to load the current URL fragment. If a route succeeds with a
760
+ // match, returns `true`. If no defined routes matches the fragment,
761
+ // returns `false`.
762
+ loadUrl : function() {
763
+ var fragment = this.fragment = this.getFragment();
764
+ var matched = _.any(this.handlers, function(handler) {
765
+ if (handler.route.test(fragment)) {
766
+ handler.callback(fragment);
767
+ return true;
768
+ }
769
+ });
770
+ return matched;
771
+ },
772
+
773
+ // Save a fragment into the hash history. You are responsible for properly
774
+ // URL-encoding the fragment in advance. This does not trigger
775
+ // a `hashchange` event.
776
+ saveLocation : function(fragment) {
777
+ fragment = (fragment || '').replace(hashStrip, '');
778
+ if (this.fragment == fragment) return;
779
+ window.location.hash = this.fragment = fragment;
780
+ if (this.iframe && (fragment != this.getFragment(this.iframe.location))) {
781
+ this.iframe.document.open().close();
782
+ this.iframe.location.hash = fragment;
783
+ }
784
+ }
785
+
786
+ });
787
+
788
+ // Backbone.View
789
+ // -------------
790
+
791
+ // Creating a Backbone.View creates its initial element outside of the DOM,
792
+ // if an existing element is not provided...
793
+ Backbone.View = function(options) {
794
+ this.cid = _.uniqueId('view');
795
+ this._configure(options || {});
796
+ this._ensureElement();
797
+ this.delegateEvents();
798
+ this.initialize(options);
799
+ };
800
+
801
+ // Element lookup, scoped to DOM elements within the current view.
802
+ // This should be prefered to global lookups, if you're dealing with
803
+ // a specific view.
804
+ var selectorDelegate = function(selector) {
805
+ return $(selector, this.el);
806
+ };
807
+
808
+ // Cached regex to split keys for `delegate`.
809
+ var eventSplitter = /^([\w|\.]+)\s*(.*)$/;
810
+
811
+ // Set up all inheritable **Backbone.View** properties and methods.
812
+ _.extend(Backbone.View.prototype, Backbone.Events, {
813
+
814
+ // The default `tagName` of a View's element is `"div"`.
815
+ tagName : 'div',
816
+
817
+ // Attach the `selectorDelegate` function as the `$` property.
818
+ $ : selectorDelegate,
819
+
820
+ // Initialize is an empty function by default. Override it with your own
821
+ // initialization logic.
822
+ initialize : function(){},
823
+
824
+ // **render** is the core function that your view should override, in order
825
+ // to populate its element (`this.el`), with the appropriate HTML. The
826
+ // convention is for **render** to always return `this`.
827
+ render : function() {
828
+ return this;
829
+ },
830
+
831
+ // Remove this view from the DOM. Note that the view isn't present in the
832
+ // DOM by default, so calling this method may be a no-op.
833
+ remove : function() {
834
+ $(this.el).remove();
835
+ return this;
836
+ },
837
+
838
+ // For small amounts of DOM Elements, where a full-blown template isn't
839
+ // needed, use **make** to manufacture elements, one at a time.
840
+ //
841
+ // var el = this.make('li', {'class': 'row'}, this.model.get('title'));
842
+ //
843
+ make : function(tagName, attributes, content) {
844
+ var el = document.createElement(tagName);
845
+ if (attributes) $(el).attr(attributes);
846
+ if (content) $(el).html(content);
847
+ return el;
848
+ },
849
+
850
+ // Set callbacks, where `this.callbacks` is a hash of
851
+ //
852
+ // *{"event selector": "callback"}*
853
+ //
854
+ // {
855
+ // 'mousedown .title': 'edit',
856
+ // 'click .button': 'save'
857
+ // }
858
+ //
859
+ // pairs. Callbacks will be bound to the view, with `this` set properly.
860
+ // Uses event delegation for efficiency.
861
+ // Omitting the selector binds the event to `this.el`.
862
+ // This only works for delegate-able events: not `focus`, `blur`, and
863
+ // not `change`, `submit`, and `reset` in Internet Explorer.
864
+ delegateEvents : function(events) {
865
+ if (!(events || (events = this.events))) return;
866
+ $(this.el).unbind('.delegateEvents' + this.cid);
867
+ for (var key in events) {
868
+ var methodName = events[key];
869
+ var match = key.match(eventSplitter);
870
+ var eventName = match[1], selector = match[2];
871
+ var method = _.bind(this[methodName], this);
872
+ eventName += '.delegateEvents' + this.cid;
873
+ if (selector === '') {
874
+ $(this.el).bind(eventName, method);
875
+ } else {
876
+ $(this.el).delegate(selector, eventName, method);
877
+ }
878
+ }
879
+ },
880
+
881
+ // Performs the initial configuration of a View with a set of options.
882
+ // Keys with special meaning *(model, collection, id, className)*, are
883
+ // attached directly to the view.
884
+ _configure : function(options) {
885
+ if (this.options) options = _.extend({}, this.options, options);
886
+ if (options.model) this.model = options.model;
887
+ if (options.collection) this.collection = options.collection;
888
+ if (options.el) this.el = options.el;
889
+ if (options.id) this.id = options.id;
890
+ if (options.className) this.className = options.className;
891
+ if (options.tagName) this.tagName = options.tagName;
892
+ this.options = options;
893
+ },
894
+
895
+ // Ensure that the View has a DOM element to render into.
896
+ // If `this.el` is a string, pass it through `$()`, take the first
897
+ // matching element, and re-assign it to `el`. Otherwise, create
898
+ // an element from the `id`, `className` and `tagName` proeprties.
899
+ _ensureElement : function() {
900
+ if (!this.el) {
901
+ var attrs = {};
902
+ if (this.id) attrs.id = this.id;
903
+ if (this.className) attrs['class'] = this.className;
904
+ this.el = this.make(this.tagName, attrs);
905
+ } else if (_.isString(this.el)) {
906
+ this.el = $(this.el).get(0);
907
+ }
908
+ }
909
+
910
+ });
911
+
912
+ // The self-propagating extend function that Backbone classes use.
913
+ var extend = function (protoProps, classProps) {
914
+ var child = inherits(this, protoProps, classProps);
915
+ child.extend = extend;
916
+ return child;
917
+ };
918
+
919
+ // Set up inheritance for the model, collection, and view.
920
+ Backbone.Model.extend = Backbone.Collection.extend =
921
+ Backbone.Controller.extend = Backbone.View.extend = extend;
922
+
923
+ // Map from CRUD to HTTP for our default `Backbone.sync` implementation.
924
+ var methodMap = {
925
+ 'create': 'POST',
926
+ 'update': 'PUT',
927
+ 'delete': 'DELETE',
928
+ 'read' : 'GET'
929
+ };
930
+
931
+ // Backbone.sync
932
+ // -------------
933
+
934
+ // Override this function to change the manner in which Backbone persists
935
+ // models to the server. You will be passed the type of request, and the
936
+ // model in question. By default, uses makes a RESTful Ajax request
937
+ // to the model's `url()`. Some possible customizations could be:
938
+ //
939
+ // * Use `setTimeout` to batch rapid-fire updates into a single request.
940
+ // * Send up the models as XML instead of JSON.
941
+ // * Persist models via WebSockets instead of Ajax.
942
+ //
943
+ // Turn on `Backbone.emulateHTTP` in order to send `PUT` and `DELETE` requests
944
+ // as `POST`, with a `_method` parameter containing the true HTTP method,
945
+ // as well as all requests with the body as `application/x-www-form-urlencoded` instead of
946
+ // `application/json` with the model in a param named `model`.
947
+ // Useful when interfacing with server-side languages like **PHP** that make
948
+ // it difficult to read the body of `PUT` requests.
949
+ Backbone.sync = function(method, model, options) {
950
+ var type = methodMap[method];
951
+
952
+ // Default JSON-request options.
953
+ var params = _.extend({
954
+ type: type,
955
+ contentType: 'application/json',
956
+ dataType: 'json',
957
+ processData: false
958
+ }, options);
959
+
960
+ // Ensure that we have a URL.
961
+ if (!params.url) {
962
+ params.url = getUrl(model) || urlError();
963
+ }
964
+
965
+ // Ensure that we have the appropriate request data.
966
+ if (!params.data && model && (method == 'create' || method == 'update')) {
967
+ params.data = JSON.stringify(model.toJSON());
968
+ }
969
+
970
+ // For older servers, emulate JSON by encoding the request into an HTML-form.
971
+ if (Backbone.emulateJSON) {
972
+ params.contentType = 'application/x-www-form-urlencoded';
973
+ params.processData = true;
974
+ params.data = params.data ? {model : params.data} : {};
975
+ }
976
+
977
+ // For older servers, emulate HTTP by mimicking the HTTP method with `_method`
978
+ // And an `X-HTTP-Method-Override` header.
979
+ if (Backbone.emulateHTTP) {
980
+ if (type === 'PUT' || type === 'DELETE') {
981
+ if (Backbone.emulateJSON) params.data._method = type;
982
+ params.type = 'POST';
983
+ params.beforeSend = function(xhr) {
984
+ xhr.setRequestHeader('X-HTTP-Method-Override', type);
985
+ };
986
+ }
987
+ }
988
+
989
+ // Make the request.
990
+ $.ajax(params);
991
+ };
992
+
993
+ // Helpers
994
+ // -------
995
+
996
+ // Shared empty constructor function to aid in prototype-chain creation.
997
+ var ctor = function(){};
998
+
999
+ // Helper function to correctly set up the prototype chain, for subclasses.
1000
+ // Similar to `goog.inherits`, but uses a hash of prototype properties and
1001
+ // class properties to be extended.
1002
+ var inherits = function(parent, protoProps, staticProps) {
1003
+ var child;
1004
+
1005
+ // The constructor function for the new subclass is either defined by you
1006
+ // (the "constructor" property in your `extend` definition), or defaulted
1007
+ // by us to simply call `super()`.
1008
+ if (protoProps && protoProps.hasOwnProperty('constructor')) {
1009
+ child = protoProps.constructor;
1010
+ } else {
1011
+ child = function(){ return parent.apply(this, arguments); };
1012
+ }
1013
+
1014
+ // Inherit class (static) properties from parent.
1015
+ _.extend(child, parent);
1016
+
1017
+ // Set the prototype chain to inherit from `parent`, without calling
1018
+ // `parent`'s constructor function.
1019
+ ctor.prototype = parent.prototype;
1020
+ child.prototype = new ctor();
1021
+
1022
+ // Add prototype properties (instance properties) to the subclass,
1023
+ // if supplied.
1024
+ if (protoProps) _.extend(child.prototype, protoProps);
1025
+
1026
+ // Add static properties to the constructor function, if supplied.
1027
+ if (staticProps) _.extend(child, staticProps);
1028
+
1029
+ // Correctly set child's `prototype.constructor`, for `instanceof`.
1030
+ child.prototype.constructor = child;
1031
+
1032
+ // Set a convenience property in case the parent's prototype is needed later.
1033
+ child.__super__ = parent.prototype;
1034
+
1035
+ return child;
1036
+ };
1037
+
1038
+ // Helper function to get a URL from a Model or Collection as a property
1039
+ // or as a function.
1040
+ var getUrl = function(object) {
1041
+ if (!(object && object.url)) return null;
1042
+ return _.isFunction(object.url) ? object.url() : object.url;
1043
+ };
1044
+
1045
+ // Throw an error when a URL is needed, and none is supplied.
1046
+ var urlError = function() {
1047
+ throw new Error("A 'url' property or function must be specified");
1048
+ };
1049
+
1050
+ // Wrap an optional error callback with a fallback error event.
1051
+ var wrapError = function(onError, model, options) {
1052
+ return function(resp) {
1053
+ if (onError) {
1054
+ onError(model, resp, options);
1055
+ } else {
1056
+ model.trigger('error', model, resp, options);
1057
+ }
1058
+ };
1059
+ };
1060
+
1061
+ // Helper function to escape a string for HTML rendering.
1062
+ var escapeHTML = function(string) {
1063
+ return string.replace(/&(?!\w+;)/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
1064
+ };
1065
+
1066
+ }).call(this);