Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / lib / backbone.js @ 7977bbc9

History | View | Annotate | Download (43.7 kB)

1
//     Backbone.js 0.5.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
  // Save a reference to the global object.
13
  var root = this;
14

    
15
  // Save the previous value of the `Backbone` variable.
16
  var previousBackbone = root.Backbone;
17

    
18
  // Create a local reference to slice.
19
  var slice = Array.prototype.slice;
20

    
21
  // The top-level namespace. All public Backbone classes and modules will
22
  // be attached to this. Exported for both CommonJS and the browser.
23
  var Backbone;
24
  if (typeof exports !== 'undefined') {
25
    Backbone = exports;
26
  } else {
27
    Backbone = root.Backbone = {};
28
  }
29

    
30
  // Current version of the library. Keep in sync with `package.json`.
31
  Backbone.VERSION = '0.5.3';
32

    
33
  // Require Underscore, if we're on the server, and it's not already present.
34
  var _ = root._;
35
  if (!_ && (typeof require !== 'undefined')) _ = require('underscore')._;
36

    
37
  // For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
38
  var $ = root.jQuery || root.Zepto || root.ender;
39

    
40
  // Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
41
  // to its previous owner. Returns a reference to this Backbone object.
42
  Backbone.noConflict = function() {
43
    root.Backbone = previousBackbone;
44
    return this;
45
  };
46

    
47
  // Turn on `emulateHTTP` to support legacy HTTP servers. Setting this option will
48
  // fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a
49
  // `X-Http-Method-Override` header.
50
  Backbone.emulateHTTP = false;
51

    
52
  // Turn on `emulateJSON` to support legacy servers that can't deal with direct
53
  // `application/json` requests ... will encode the body as
54
  // `application/x-www-form-urlencoded` instead and will send the model in a
55
  // form param named `model`.
56
  Backbone.emulateJSON = false;
57

    
58
  // Backbone.Events
59
  // -----------------
60

    
61
  // A module that can be mixed in to *any object* in order to provide it with
62
  // custom events. You may `bind` or `unbind` a callback function to an event;
63
  // `trigger`-ing an event fires all callbacks in succession.
64
  //
65
  //     var object = {};
66
  //     _.extend(object, Backbone.Events);
67
  //     object.bind('expand', function(){ alert('expanded'); });
68
  //     object.trigger('expand');
69
  //
70
  Backbone.Events = {
71

    
72
    // Bind an event, specified by a string name, `ev`, to a `callback` function.
73
    // Passing `"all"` will bind the callback to all events fired.
74
    bind : function(ev, callback, context) {
75
      var calls = this._callbacks || (this._callbacks = {});
76
      var list  = calls[ev] || (calls[ev] = {});
77
      var tail = list.tail || (list.tail = list.next = {});
78
      tail.callback = callback;
79
      tail.context = context;
80
      list.tail = tail.next = {};
81
      return this;
82
    },
83

    
84
    // Remove one or many callbacks. If `callback` is null, removes all
85
    // callbacks for the event. If `ev` is null, removes all bound callbacks
86
    // for all events.
87
    unbind : function(ev, callback) {
88
      var calls, node, prev;
89
      if (!ev) {
90
        this._callbacks = null;
91
      } else if (calls = this._callbacks) {
92
        if (!callback) {
93
          calls[ev] = {};
94
        } else if (node = calls[ev]) {
95
          while ((prev = node) && (node = node.next)) {
96
            if (node.callback !== callback) continue;
97
            prev.next = node.next;
98
            node.context = node.callback = null;
99
            break;
100
          }
101
        }
102
      }
103
      return this;
104
    },
105

    
106
    // Trigger an event, firing all bound callbacks. Callbacks are passed the
107
    // same arguments as `trigger` is, apart from the event name.
108
    // Listening for `"all"` passes the true event name as the first argument.
109
    trigger : function(eventName) {
110
      var node, calls, callback, args, ev, events = ['all', eventName];
111
      if (!(calls = this._callbacks)) return this;
112
      while (ev = events.pop()) {
113
        if (!(node = calls[ev])) continue;
114
        args = ev == 'all' ? arguments : slice.call(arguments, 1);
115
        while (node = node.next) if (callback = node.callback) callback.apply(node.context || this, args);
116
      }
117
      return this;
118
    }
119

    
120
  };
121

    
122
  // Backbone.Model
123
  // --------------
124

    
125
  // Create a new model, with defined attributes. A client id (`cid`)
126
  // is automatically generated and assigned for you.
127
  Backbone.Model = function(attributes, options) {
128
    var defaults;
129
    attributes || (attributes = {});
130
    if (defaults = this.defaults) {
131
      if (_.isFunction(defaults)) defaults = defaults.call(this);
132
      attributes = _.extend({}, defaults, attributes);
133
    }
134
    this.attributes = {};
135
    this._escapedAttributes = {};
136
    this.cid = _.uniqueId('c');
137
    this.set(attributes, {silent : true});
138
    this._changed = false;
139
    this._previousAttributes = _.clone(this.attributes);
140
    if (options && options.collection) this.collection = options.collection;
141
    this.initialize(attributes, options);
142
  };
143

    
144
  // Attach all inheritable methods to the Model prototype.
145
  _.extend(Backbone.Model.prototype, Backbone.Events, {
146

    
147
    // Has the item been changed since the last `"change"` event?
148
    _changed : false,
149

    
150
    // The default name for the JSON `id` attribute is `"id"`. MongoDB and
151
    // CouchDB users may want to set this to `"_id"`.
152
    idAttribute : 'id',
153

    
154
    // Initialize is an empty function by default. Override it with your own
155
    // initialization logic.
156
    initialize : function(){},
157

    
158
    // Return a copy of the model's `attributes` object.
159
    toJSON : function() {
160
      return _.clone(this.attributes);
161
    },
162

    
163
    // Get the value of an attribute.
164
    get : function(attr) {
165
      return this.attributes[attr];
166
    },
167

    
168
    // Get the HTML-escaped value of an attribute.
169
    escape : function(attr) {
170
      var html;
171
      if (html = this._escapedAttributes[attr]) return html;
172
      var val = this.attributes[attr];
173
      return this._escapedAttributes[attr] = _.escape(val == null ? '' : '' + val);
174
    },
175

    
176
    // Returns `true` if the attribute contains a value that is not null
177
    // or undefined.
178
    has : function(attr) {
179
      return this.attributes[attr] != null;
180
    },
181

    
182
    // Set a hash of model attributes on the object, firing `"change"` unless you
183
    // choose to silence it.
184
    set : function(attrs, options) {
185

    
186
      // Extract attributes and options.
187
      options || (options = {});
188
      if (!attrs) return this;
189
      if (attrs.attributes) attrs = attrs.attributes;
190
      var now = this.attributes, escaped = this._escapedAttributes;
191

    
192
      // Run validation.
193
      if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;
194

    
195
      // Check for changes of `id`.
196
      if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
197

    
198
      // We're about to start triggering change events.
199
      var alreadyChanging = this._changing;
200
      this._changing = true;
201

    
202
      // Update attributes.
203
      for (var attr in attrs) {
204
        var val = attrs[attr];
205
        if (!_.isEqual(now[attr], val)) {
206
          now[attr] = val;
207
          delete escaped[attr];
208
          this._changed = true;
209
          if (!options.silent) this.trigger('change:' + attr, this, val, options);
210
        }
211
      }
212

    
213
      // Fire the `"change"` event, if the model has been changed.
214
      if (!alreadyChanging && !options.silent && this._changed) this.change(options);
215
      this._changing = false;
216
      return this;
217
    },
218

    
219
    // Remove an attribute from the model, firing `"change"` unless you choose
220
    // to silence it. `unset` is a noop if the attribute doesn't exist.
221
    unset : function(attr, options) {
222
      if (!(attr in this.attributes)) return this;
223
      options || (options = {});
224
      var value = this.attributes[attr];
225

    
226
      // Run validation.
227
      var validObj = {};
228
      validObj[attr] = void 0;
229
      if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
230

    
231
      // changedAttributes needs to know if an attribute has been unset.
232
      (this._unsetAttributes || (this._unsetAttributes = [])).push(attr);
233

    
234
      // Remove the attribute.
235
      delete this.attributes[attr];
236
      delete this._escapedAttributes[attr];
237
      if (attr == this.idAttribute) delete this.id;
238
      this._changed = true;
239
      if (!options.silent) {
240
        this.trigger('change:' + attr, this, void 0, options);
241
        this.change(options);
242
      }
243
      return this;
244
    },
245

    
246
    // Clear all attributes on the model, firing `"change"` unless you choose
247
    // to silence it.
248
    clear : function(options) {
249
      options || (options = {});
250
      var attr;
251
      var old = this.attributes;
252

    
253
      // Run validation.
254
      var validObj = {};
255
      for (attr in old) validObj[attr] = void 0;
256
      if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
257

    
258
      this.attributes = {};
259
      this._escapedAttributes = {};
260
      this._changed = true;
261
      if (!options.silent) {
262
        for (attr in old) {
263
          this.trigger('change:' + attr, this, void 0, options);
264
        }
265
        this.change(options);
266
      }
267
      return this;
268
    },
269

    
270
    // Fetch the model from the server. If the server's representation of the
271
    // model differs from its current attributes, they will be overriden,
272
    // triggering a `"change"` event.
273
    fetch : function(options) {
274
      options || (options = {});
275
      var model = this;
276
      var success = options.success;
277
      options.success = function(resp, status, xhr) {
278
        if (!model.set(model.parse(resp, xhr), options)) return false;
279
        if (success) success(model, resp);
280
      };
281
      options.error = wrapError(options.error, model, options);
282
      return (this.sync || Backbone.sync).call(this, 'read', this, options);
283
    },
284

    
285
    // Set a hash of model attributes, and sync the model to the server.
286
    // If the server returns an attributes hash that differs, the model's
287
    // state will be `set` again.
288
    save : function(attrs, options) {
289
      options || (options = {});
290
      if (attrs && !this.set(attrs, options)) return false;
291
      var model = this;
292
      var success = options.success;
293
      options.success = function(resp, status, xhr) {
294
        if (!model.set(model.parse(resp, xhr), options)) return false;
295
        if (success) success(model, resp, xhr);
296
      };
297
      options.error = wrapError(options.error, model, options);
298
      var method = this.isNew() ? 'create' : 'update';
299
      return (this.sync || Backbone.sync).call(this, method, this, options);
300
    },
301

    
302
    // Destroy this model on the server if it was already persisted. Upon success, the model is removed
303
    // from its collection, if it has one.
304
    destroy : function(options) {
305
      options || (options = {});
306
      if (this.isNew()) return this.trigger('destroy', this, this.collection, options);
307
      var model = this;
308
      var success = options.success;
309
      options.success = function(resp) {
310
        if (!options.silent) {
311
          model.trigger('destroy', model, model.collection, options);
312
        }
313
        if (success) success(model, resp);
314
      };
315
      options.error = wrapError(options.error, model, options);
316
      return (this.sync || Backbone.sync).call(this, 'delete', this, options);
317
    },
318

    
319
    // Default URL for the model's representation on the server -- if you're
320
    // using Backbone's restful methods, override this to change the endpoint
321
    // that will be called.
322
    url : function(options) {
323
      var base = getUrl(this.collection, options) || this.urlRoot || urlError();
324
      if (this.isNew()) return base;
325
      return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
326
    },
327

    
328
    // **parse** converts a response into the hash of attributes to be `set` on
329
    // the model. The default implementation is just to pass the response along.
330
    parse : function(resp, xhr) {
331
      return resp;
332
    },
333

    
334
    // Create a new model with identical attributes to this one.
335
    clone : function() {
336
      return new this.constructor(this);
337
    },
338

    
339
    // A model is new if it has never been saved to the server, and lacks an id.
340
    isNew : function() {
341
      return this.id == null;
342
    },
343

    
344
    // Call this method to manually fire a `change` event for this model.
345
    // Calling this will cause all objects observing the model to update.
346
    change : function(options) {
347
      this.trigger('change', this, options);
348
      this._previousAttributes = _.clone(this.attributes);
349
      this._unsetAttributes = null;
350
      this._changed = false;
351
    },
352

    
353
    // Determine if the model has changed since the last `"change"` event.
354
    // If you specify an attribute name, determine if that attribute has changed.
355
    hasChanged : function(attr) {
356
      if (attr) return this._previousAttributes[attr] != this.attributes[attr];
357
      return this._changed;
358
    },
359

    
360
    // Return an object containing all the attributes that have changed, or false
361
    // if there are no changed attributes. Useful for determining what parts of a
362
    // view need to be updated and/or what attributes need to be persisted to
363
    // the server. Unset attributes will be set to undefined.
364
    changedAttributes : function(now) {
365
      now || (now = this.attributes);
366
      var old = this._previousAttributes, unset = this._unsetAttributes;
367

    
368
      var changed = false;
369
      for (var attr in now) {
370
        if (!_.isEqual(old[attr], now[attr])) {
371
          changed || (changed = {});
372
          changed[attr] = now[attr];
373
        }
374
      }
375

    
376
      if (unset) {
377
        changed || (changed = {});
378
        var len = unset.length;
379
        while (len--) changed[unset[len]] = void 0;
380
      }
381

    
382
      return changed;
383
    },
384

    
385
    // Get the previous value of an attribute, recorded at the time the last
386
    // `"change"` event was fired.
387
    previous : function(attr) {
388
      if (!attr || !this._previousAttributes) return null;
389
      return this._previousAttributes[attr];
390
    },
391

    
392
    // Get all of the attributes of the model at the time of the previous
393
    // `"change"` event.
394
    previousAttributes : function() {
395
      return _.clone(this._previousAttributes);
396
    },
397

    
398
    // Run validation against a set of incoming attributes, returning `true`
399
    // if all is well. If a specific `error` callback has been passed,
400
    // call that instead of firing the general `"error"` event.
401
    _performValidation : function(attrs, options) {
402
      var error = this.validate(attrs);
403
      if (error) {
404
        if (options.error) {
405
          options.error(this, error, options);
406
        } else {
407
          this.trigger('error', this, error, options);
408
        }
409
        return false;
410
      }
411
      return true;
412
    }
413

    
414
  });
415

    
416
  // Backbone.Collection
417
  // -------------------
418

    
419
  // Provides a standard collection class for our sets of models, ordered
420
  // or unordered. If a `comparator` is specified, the Collection will maintain
421
  // its models in sort order, as they're added and removed.
422
  Backbone.Collection = function(models, options) {
423
    options || (options = {});
424
    if (options.comparator) this.comparator = options.comparator;
425
    _.bindAll(this, '_onModelEvent', '_removeReference');
426
    this._reset();
427
    if (models) this.reset(models, {silent: true});
428
    this.initialize.apply(this, arguments);
429
  };
430

    
431
  // Define the Collection's inheritable methods.
432
  _.extend(Backbone.Collection.prototype, Backbone.Events, {
433

    
434
    // The default model for a collection is just a **Backbone.Model**.
435
    // This should be overridden in most cases.
436
    model : Backbone.Model,
437

    
438
    // Initialize is an empty function by default. Override it with your own
439
    // initialization logic.
440
    initialize : function(){},
441

    
442
    // The JSON representation of a Collection is an array of the
443
    // models' attributes.
444
    toJSON : function() {
445
      return this.map(function(model){ return model.toJSON(); });
446
    },
447

    
448
    // Add a model, or list of models to the set. Pass **silent** to avoid
449
    // firing the `added` event for every new model.
450
    add : function(models, options) {
451
      if (_.isArray(models)) {
452
        for (var i = 0, l = models.length; i < l; i++) {
453
          this._add(models[i], options);
454
        }
455
      } else {
456
        this._add(models, options);
457
      }
458
      return this;
459
    },
460

    
461
    // Remove a model, or a list of models from the set. Pass silent to avoid
462
    // firing the `removed` event for every model removed.
463
    remove : function(models, options) {
464
      if (_.isArray(models)) {
465
        for (var i = 0, l = models.length; i < l; i++) {
466
          this._remove(models[i], options);
467
        }
468
      } else {
469
        this._remove(models, options);
470
      }
471
      return this;
472
    },
473

    
474
    // Get a model from the set by id.
475
    get : function(id) {
476
      if (id == null) return null;
477
      return this._byId[id.id != null ? id.id : id];
478
    },
479

    
480
    // Get a model from the set by client id.
481
    getByCid : function(cid) {
482
      return cid && this._byCid[cid.cid || cid];
483
    },
484

    
485
    // Get the model at the given index.
486
    at : function(index) {
487
      return this.models[index];
488
    },
489

    
490
    // Force the collection to re-sort itself. You don't need to call this under normal
491
    // circumstances, as the set will maintain sort order as each item is added.
492
    sort : function(options) {
493
      options || (options = {});
494
      if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
495
      this.models = this.sortBy(this.comparator);
496
      if (!options.silent) this.trigger('reset', this, options);
497
      return this;
498
    },
499

    
500
    // Pluck an attribute from each model in the collection.
501
    pluck : function(attr) {
502
      return _.map(this.models, function(model){ return model.get(attr); });
503
    },
504

    
505
    // When you have more items than you want to add or remove individually,
506
    // you can reset the entire set with a new list of models, without firing
507
    // any `added` or `removed` events. Fires `reset` when finished.
508
    reset : function(models, options) {
509
      models  || (models = []);
510
      options || (options = {});
511
      this.each(this._removeReference);
512
      this._reset();
513
      this.add(models, {silent: true});
514
      if (!options.silent) this.trigger('reset', this, options);
515
      return this;
516
    },
517

    
518
    // When you have an existing set of models in a collection, 
519
    // you can do in-place updates of these models, reusing existing instances.
520
    // - Items are matched against existing items in the collection by id
521
    // - New items are added
522
    // - matching models are updated using set(), triggers 'change'.
523
    // - existing models not present in the update are removed if 'removeMissing' is passed.
524
    // - a collection change event will be dispatched for each add() and remove()
525
    update : function(models, options) {
526
      models  || (models = []);
527
      options || (options = {});
528

    
529
      //keep track of the models we've updated, cause we're gunna delete the rest if 'removeMissing' is set.
530
      var updateMap = _.reduce(this.models, function(map, model){ map[model.id] = false; return map },{});
531

    
532
      _.each( models, function(model) {
533

    
534
        var idAttribute = this.model.prototype.idAttribute;
535
        var modelId = model[idAttribute];
536

    
537
        if ( modelId == undefined ) throw new Error("Can't update a model with no id attribute. Please use 'reset'.");
538
        
539
        if ( this._byId[modelId] ) {
540
          var attrs = (model instanceof Backbone.Model) ? _.clone(model.attributes) : _.clone(model);
541
          delete attrs[idAttribute];
542
          this._byId[modelId].set( attrs );
543
          updateMap[modelId] = true;
544
        }
545
        else {
546
          this.add( model );
547
        }
548
      }, this);
549

    
550
      if ( options.removeMissing ) {
551
        _.select(updateMap, function(updated, modelId){
552
          if (!updated) this.remove( modelId );
553
        }, this);
554
      }
555

    
556
      return this;
557
    },
558

    
559
    // Fetch the default set of models for this collection, resetting the
560
    // collection when they arrive. If `add: true` is passed, appends the
561
    // models to the collection instead of resetting.
562
    fetch : function(options) {
563
      options || (options = {});
564
      var collection = this;
565
      var success = options.success;
566
      options.success = function(resp, status, xhr) {
567
        collection[options.update ? 'update' : options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
568
        if (success) success(collection, resp);
569
      };
570
      options.error = wrapError(options.error, collection, options);
571
      return (this.sync || Backbone.sync).call(this, 'read', this, options);
572
    },
573

    
574
    // Create a new instance of a model in this collection. After the model
575
    // has been created on the server, it will be added to the collection.
576
    // Returns the model, or 'false' if validation on a new model fails.
577
    create : function(model, options) {
578
      var coll = this;
579
      options || (options = {});
580
      model = this._prepareModel(model, options);
581
      if (!model) return false;
582
      var success = options.success;
583
      options.success = function(nextModel, resp, xhr) {
584
        coll.add(nextModel, options);
585
        if (success) success(nextModel, resp, xhr);
586
      };
587
      model.save(null, options);
588
      return model;
589
    },
590

    
591
    // **parse** converts a response into a list of models to be added to the
592
    // collection. The default implementation is just to pass it through.
593
    parse : function(resp, xhr) {
594
      return resp;
595
    },
596

    
597
    // Proxy to _'s chain. Can't be proxied the same way the rest of the
598
    // underscore methods are proxied because it relies on the underscore
599
    // constructor.
600
    chain : function () {
601
      return _(this.models).chain();
602
    },
603

    
604
    // Reset all internal state. Called when the collection is reset.
605
    _reset : function(options) {
606
      this.length = 0;
607
      this.models = [];
608
      this._byId  = {};
609
      this._byCid = {};
610
    },
611

    
612
    // Prepare a model to be added to this collection
613
    _prepareModel : function(model, options) {
614
      if (!(model instanceof Backbone.Model)) {
615
        var attrs = model;
616
        model = new this.model(attrs, {collection: this});
617
        if (model.validate && !model._performValidation(model.attributes, options)) model = false;
618
      } else if (!model.collection) {
619
        model.collection = this;
620
      }
621
      return model;
622
    },
623

    
624
    // Internal implementation of adding a single model to the set, updating
625
    // hash indexes for `id` and `cid` lookups.
626
    // Returns the model, or 'false' if validation on a new model fails.
627
    _add : function(model, options) {
628
      options || (options = {});
629
      model = this._prepareModel(model, options);
630
      if (!model) return false;
631
      var already = this.getByCid(model);
632
      if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
633
      this._byId[model.id] = model;
634
      this._byCid[model.cid] = model;
635
      var index = options.at != null ? options.at :
636
                  this.comparator ? this.sortedIndex(model, this.comparator) :
637
                  this.length;
638
      this.models.splice(index, 0, model);
639
      model.bind('all', this._onModelEvent);
640
      this.length++;
641
      options.index = index;
642
      if (!options.silent) model.trigger('add', model, this, options);
643
      return model;
644
    },
645

    
646
    // Internal implementation of removing a single model from the set, updating
647
    // hash indexes for `id` and `cid` lookups.
648
    _remove : function(model, options) {
649
      options || (options = {});
650
      model = this.getByCid(model) || this.get(model);
651
      if (!model) return null;
652
      delete this._byId[model.id];
653
      delete this._byCid[model.cid];
654
      var index = this.indexOf(model);
655
      this.models.splice(index, 1);
656
      this.length--;
657
      options.index = index;
658
      if (!options.silent) model.trigger('remove', model, this, options);
659
      this._removeReference(model);
660
      return model;
661
    },
662

    
663
    // Internal method to remove a model's ties to a collection.
664
    _removeReference : function(model) {
665
      if (this == model.collection) {
666
        delete model.collection;
667
      }
668
      model.unbind('all', this._onModelEvent);
669
    },
670

    
671
    // Internal method called every time a model in the set fires an event.
672
    // Sets need to update their indexes when models change ids. All other
673
    // events simply proxy through. "add" and "remove" events that originate
674
    // in other collections are ignored.
675
    _onModelEvent : function(ev, model, collection, options) {
676
      if ((ev == 'add' || ev == 'remove') && collection != this) return;
677
      if (ev == 'destroy') {
678
        this._remove(model, options);
679
      }
680
      if (model && ev === 'change:' + model.idAttribute) {
681
        delete this._byId[model.previous(model.idAttribute)];
682
        this._byId[model.id] = model;
683
      }
684
      this.trigger.apply(this, arguments);
685
    }
686

    
687
  });
688

    
689
  // Underscore methods that we want to implement on the Collection.
690
  var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find', 'detect',
691
    'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 'include',
692
    'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex', 'toArray', 'size',
693
    'first', 'rest', 'last', 'without', 'indexOf', 'lastIndexOf', 'isEmpty', 'groupBy'];
694

    
695
  // Mix in each Underscore method as a proxy to `Collection#models`.
696
  _.each(methods, function(method) {
697
    Backbone.Collection.prototype[method] = function() {
698
      return _[method].apply(_, [this.models].concat(_.toArray(arguments)));
699
    };
700
  });
701

    
702
  // Backbone.Router
703
  // -------------------
704

    
705
  // Routers map faux-URLs to actions, and fire events when routes are
706
  // matched. Creating a new one sets its `routes` hash, if not set statically.
707
  Backbone.Router = function(options) {
708
    options || (options = {});
709
    if (options.routes) this.routes = options.routes;
710
    this._bindRoutes();
711
    this.initialize.apply(this, arguments);
712
  };
713

    
714
  // Cached regular expressions for matching named param parts and splatted
715
  // parts of route strings.
716
  var namedParam    = /:([\w\d]+)/g;
717
  var splatParam    = /\*([\w\d]+)/g;
718
  var escapeRegExp  = /[-[\]{}()+?.,\\^$|#\s]/g;
719

    
720
  // Set up all inheritable **Backbone.Router** properties and methods.
721
  _.extend(Backbone.Router.prototype, Backbone.Events, {
722

    
723
    // Initialize is an empty function by default. Override it with your own
724
    // initialization logic.
725
    initialize : function(){},
726

    
727
    // Manually bind a single named route to a callback. For example:
728
    //
729
    //     this.route('search/:query/p:num', 'search', function(query, num) {
730
    //       ...
731
    //     });
732
    //
733
    route : function(route, name, callback) {
734
      Backbone.history || (Backbone.history = new Backbone.History);
735
      if (!_.isRegExp(route)) route = this._routeToRegExp(route);
736
      Backbone.history.route(route, _.bind(function(fragment) {
737
        var args = this._extractParameters(route, fragment);
738
        callback && callback.apply(this, args);
739
        this.trigger.apply(this, ['route:' + name].concat(args));
740
      }, this));
741
    },
742

    
743
    // Simple proxy to `Backbone.history` to save a fragment into the history.
744
    navigate : function(fragment, triggerRoute) {
745
      Backbone.history.navigate(fragment, triggerRoute);
746
    },
747

    
748
    // Bind all defined routes to `Backbone.history`. We have to reverse the
749
    // order of the routes here to support behavior where the most general
750
    // routes can be defined at the bottom of the route map.
751
    _bindRoutes : function() {
752
      if (!this.routes) return;
753
      var routes = [];
754
      for (var route in this.routes) {
755
        routes.unshift([route, this.routes[route]]);
756
      }
757
      for (var i = 0, l = routes.length; i < l; i++) {
758
        this.route(routes[i][0], routes[i][1], this[routes[i][1]]);
759
      }
760
    },
761

    
762
    // Convert a route string into a regular expression, suitable for matching
763
    // against the current location hash.
764
    _routeToRegExp : function(route) {
765
      route = route.replace(escapeRegExp, "\\$&")
766
                   .replace(namedParam, "([^\/]*)")
767
                   .replace(splatParam, "(.*?)");
768
      return new RegExp('^' + route + '$');
769
    },
770

    
771
    // Given a route, and a URL fragment that it matches, return the array of
772
    // extracted parameters.
773
    _extractParameters : function(route, fragment) {
774
      return route.exec(fragment).slice(1);
775
    }
776

    
777
  });
778

    
779
  // Backbone.History
780
  // ----------------
781

    
782
  // Handles cross-browser history management, based on URL fragments. If the
783
  // browser does not support `onhashchange`, falls back to polling.
784
  Backbone.History = function() {
785
    this.handlers = [];
786
    _.bindAll(this, 'checkUrl');
787
  };
788

    
789
  // Cached regex for cleaning hashes.
790
  var hashStrip = /^#*/;
791

    
792
  // Cached regex for detecting MSIE.
793
  var isExplorer = /msie [\w.]+/;
794

    
795
  // Has the history handling already been started?
796
  var historyStarted = false;
797

    
798
  // Set up all inheritable **Backbone.History** properties and methods.
799
  _.extend(Backbone.History.prototype, {
800

    
801
    // The default interval to poll for hash changes, if necessary, is
802
    // twenty times a second.
803
    interval: 50,
804

    
805
    // Get the cross-browser normalized URL fragment, either from the URL,
806
    // the hash, or the override.
807
    getFragment : function(fragment, forcePushState) {
808
      if (fragment == null) {
809
        if (this._hasPushState || forcePushState) {
810
          fragment = window.location.pathname;
811
          var search = window.location.search;
812
          if (search) fragment += search;
813
        } else {
814
          fragment = window.location.hash;
815
        }
816
      }
817
      fragment = decodeURIComponent(fragment.replace(hashStrip, ''));
818
      if (!fragment.indexOf(this.options.root)) fragment = fragment.substr(this.options.root.length);
819
      return fragment;
820
    },
821

    
822
    // Start the hash change handling, returning `true` if the current URL matches
823
    // an existing route, and `false` otherwise.
824
    start : function(options) {
825

    
826
      // Figure out the initial configuration. Do we need an iframe?
827
      // Is pushState desired ... is it available?
828
      if (historyStarted) throw new Error("Backbone.history has already been started");
829
      this.options          = _.extend({}, {root: '/'}, this.options, options);
830
      this._wantsPushState  = !!this.options.pushState;
831
      this._hasPushState    = !!(this.options.pushState && window.history && window.history.pushState);
832
      var fragment          = this.getFragment();
833
      var docMode           = document.documentMode;
834
      var oldIE             = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7));
835
      if (oldIE) {
836
        this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
837
        this.navigate(fragment);
838
      }
839

    
840
      // Depending on whether we're using pushState or hashes, and whether
841
      // 'onhashchange' is supported, determine how we check the URL state.
842
      if (this._hasPushState) {
843
        $(window).bind('popstate', this.checkUrl);
844
      } else if ('onhashchange' in window && !oldIE) {
845
        $(window).bind('hashchange', this.checkUrl);
846
      } else {
847
        setInterval(this.checkUrl, this.interval);
848
      }
849

    
850
      // Determine if we need to change the base url, for a pushState link
851
      // opened by a non-pushState browser.
852
      this.fragment = fragment;
853
      historyStarted = true;
854
      var loc = window.location;
855
      var atRoot  = loc.pathname == this.options.root;
856
      if (this._wantsPushState && !this._hasPushState && !atRoot) {
857
        this.fragment = this.getFragment(null, true);
858
        window.location.replace(this.options.root + '#' + this.fragment);
859
        // Return immediately as browser will do redirect to new url
860
        return true;
861
      } else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
862
        this.fragment = loc.hash.replace(hashStrip, '');
863
        window.history.replaceState({}, document.title, loc.protocol + '//' + loc.host + this.options.root + this.fragment);
864
      }
865

    
866
      if (!this.options.silent) {
867
        return this.loadUrl();
868
      }
869
    },
870

    
871
    // Add a route to be tested when the fragment changes. Routes added later may
872
    // override previous routes.
873
    route : function(route, callback) {
874
      this.handlers.unshift({route : route, callback : callback});
875
    },
876

    
877
    // Checks the current URL to see if it has changed, and if it has,
878
    // calls `loadUrl`, normalizing across the hidden iframe.
879
    checkUrl : function(e) {
880
      var current = this.getFragment();
881
      if (current == this.fragment && this.iframe) current = this.getFragment(this.iframe.location.hash);
882
      if (current == this.fragment || current == decodeURIComponent(this.fragment)) return false;
883
      if (this.iframe) this.navigate(current);
884
      this.loadUrl() || this.loadUrl(window.location.hash);
885
    },
886

    
887
    // Attempt to load the current URL fragment. If a route succeeds with a
888
    // match, returns `true`. If no defined routes matches the fragment,
889
    // returns `false`.
890
    loadUrl : function(fragmentOverride) {
891
      var fragment = this.fragment = this.getFragment(fragmentOverride);
892
      var matched = _.any(this.handlers, function(handler) {
893
        if (handler.route.test(fragment)) {
894
          handler.callback(fragment);
895
          return true;
896
        }
897
      });
898
      return matched;
899
    },
900

    
901
    // Save a fragment into the hash history. You are responsible for properly
902
    // URL-encoding the fragment in advance. This does not trigger
903
    // a `hashchange` event.
904
    navigate : function(fragment, triggerRoute) {
905
      var frag = (fragment || '').replace(hashStrip, '');
906
      if (this.fragment == frag || this.fragment == decodeURIComponent(frag)) return;
907
      if (this._hasPushState) {
908
        var loc = window.location;
909
        if (frag.indexOf(this.options.root) != 0) frag = this.options.root + frag;
910
        this.fragment = frag;
911
        window.history.pushState({}, document.title, loc.protocol + '//' + loc.host + frag);
912
      } else {
913
        window.location.hash = this.fragment = frag;
914
        if (this.iframe && (frag != this.getFragment(this.iframe.location.hash))) {
915
          this.iframe.document.open().close();
916
          this.iframe.location.hash = frag;
917
        }
918
      }
919
      if (triggerRoute) this.loadUrl(fragment);
920
    }
921

    
922
  });
923

    
924
  // Backbone.View
925
  // -------------
926

    
927
  // Creating a Backbone.View creates its initial element outside of the DOM,
928
  // if an existing element is not provided...
929
  Backbone.View = function(options) {
930
    this.cid = _.uniqueId('view');
931
    this._configure(options || {});
932
    this._ensureElement();
933
    this.delegateEvents();
934
    this.initialize.apply(this, arguments);
935
  };
936

    
937
  // Element lookup, scoped to DOM elements within the current view.
938
  // This should be prefered to global lookups, if you're dealing with
939
  // a specific view.
940
  var selectorDelegate = function(selector) {
941
    return $(selector, this.el);
942
  };
943

    
944
  // Cached regex to split keys for `delegate`.
945
  var eventSplitter = /^(\S+)\s*(.*)$/;
946

    
947
  // List of view options to be merged as properties.
948
  var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];
949

    
950
  // Set up all inheritable **Backbone.View** properties and methods.
951
  _.extend(Backbone.View.prototype, Backbone.Events, {
952

    
953
    // The default `tagName` of a View's element is `"div"`.
954
    tagName : 'div',
955

    
956
    // Attach the `selectorDelegate` function as the `$` property.
957
    $       : selectorDelegate,
958

    
959
    // Initialize is an empty function by default. Override it with your own
960
    // initialization logic.
961
    initialize : function(){},
962

    
963
    // **render** is the core function that your view should override, in order
964
    // to populate its element (`this.el`), with the appropriate HTML. The
965
    // convention is for **render** to always return `this`.
966
    render : function() {
967
      return this;
968
    },
969

    
970
    // Remove this view from the DOM. Note that the view isn't present in the
971
    // DOM by default, so calling this method may be a no-op.
972
    remove : function() {
973
      $(this.el).remove();
974
      return this;
975
    },
976

    
977
    // For small amounts of DOM Elements, where a full-blown template isn't
978
    // needed, use **make** to manufacture elements, one at a time.
979
    //
980
    //     var el = this.make('li', {'class': 'row'}, this.model.escape('title'));
981
    //
982
    make : function(tagName, attributes, content) {
983
      var el = document.createElement(tagName);
984
      if (attributes) $(el).attr(attributes);
985
      if (content) $(el).html(content);
986
      return el;
987
    },
988

    
989
    // Set callbacks, where `this.events` is a hash of
990
    //
991
    // *{"event selector": "callback"}*
992
    //
993
    //     {
994
    //       'mousedown .title':  'edit',
995
    //       'click .button':     'save'
996
    //     }
997
    //
998
    // pairs. Callbacks will be bound to the view, with `this` set properly.
999
    // Uses event delegation for efficiency.
1000
    // Omitting the selector binds the event to `this.el`.
1001
    // This only works for delegate-able events: not `focus`, `blur`, and
1002
    // not `change`, `submit`, and `reset` in Internet Explorer.
1003
    delegateEvents : function(events) {
1004
      if (!(events || (events = this.events))) return;
1005
      if (_.isFunction(events)) events = events.call(this);
1006
      this.undelegateEvents();
1007
      for (var key in events) {
1008
        var method = this[events[key]];
1009
        if (!method) throw new Error('Event "' + events[key] + '" does not exist');
1010
        var match = key.match(eventSplitter);
1011
        var eventName = match[1], selector = match[2];
1012
        method = _.bind(method, this);
1013
        eventName += '.delegateEvents' + this.cid;
1014
        if (selector === '') {
1015
          $(this.el).bind(eventName, method);
1016
        } else {
1017
          $(this.el).delegate(selector, eventName, method);
1018
        }
1019
      }
1020
    },
1021

    
1022
    // Clears all callbacks previously bound to the view with `delegateEvents`.
1023
    undelegateEvents : function() {
1024
      $(this.el).unbind('.delegateEvents' + this.cid);
1025
    },
1026

    
1027
    // Performs the initial configuration of a View with a set of options.
1028
    // Keys with special meaning *(model, collection, id, className)*, are
1029
    // attached directly to the view.
1030
    _configure : function(options) {
1031
      if (this.options) options = _.extend({}, this.options, options);
1032
      for (var i = 0, l = viewOptions.length; i < l; i++) {
1033
        var attr = viewOptions[i];
1034
        if (options[attr]) this[attr] = options[attr];
1035
      }
1036
      this.options = options;
1037
    },
1038

    
1039
    // Ensure that the View has a DOM element to render into.
1040
    // If `this.el` is a string, pass it through `$()`, take the first
1041
    // matching element, and re-assign it to `el`. Otherwise, create
1042
    // an element from the `id`, `className` and `tagName` properties.
1043
    _ensureElement : function() {
1044
      if (!this.el) {
1045
        var attrs = this.attributes || {};
1046
        if (this.id) attrs.id = this.id;
1047
        if (this.className) attrs['class'] = this.className;
1048
        this.el = this.make(this.tagName, attrs);
1049
      } else if (_.isString(this.el)) {
1050
        this.el = $(this.el).get(0);
1051
      }
1052
    }
1053

    
1054
  });
1055

    
1056
  // The self-propagating extend function that Backbone classes use.
1057
  var extend = function (protoProps, classProps) {
1058
    var child = inherits(this, protoProps, classProps);
1059
    child.extend = this.extend;
1060
    return child;
1061
  };
1062

    
1063
  // Set up inheritance for the model, collection, and view.
1064
  Backbone.Model.extend = Backbone.Collection.extend =
1065
    Backbone.Router.extend = Backbone.View.extend = extend;
1066

    
1067
  // Map from CRUD to HTTP for our default `Backbone.sync` implementation.
1068
  var methodMap = {
1069
    'create': 'POST',
1070
    'update': 'PUT',
1071
    'delete': 'DELETE',
1072
    'read'  : 'GET',
1073
    'head'  : 'HEAD'
1074
  };
1075

    
1076
  // Backbone.sync
1077
  // -------------
1078

    
1079
  // Override this function to change the manner in which Backbone persists
1080
  // models to the server. You will be passed the type of request, and the
1081
  // model in question. By default, makes a RESTful Ajax request
1082
  // to the model's `url()`. Some possible customizations could be:
1083
  //
1084
  // * Use `setTimeout` to batch rapid-fire updates into a single request.
1085
  // * Send up the models as XML instead of JSON.
1086
  // * Persist models via WebSockets instead of Ajax.
1087
  //
1088
  // Turn on `Backbone.emulateHTTP` in order to send `PUT` and `DELETE` requests
1089
  // as `POST`, with a `_method` parameter containing the true HTTP method,
1090
  // as well as all requests with the body as `application/x-www-form-urlencoded` instead of
1091
  // `application/json` with the model in a param named `model`.
1092
  // Useful when interfacing with server-side languages like **PHP** that make
1093
  // it difficult to read the body of `PUT` requests.
1094
  Backbone.sync = function(method, model, options) {
1095
    var type = methodMap[method];
1096

    
1097
    // Default JSON-request options.
1098
    var params = {type : type, dataType : 'json'};
1099

    
1100
    // Ensure that we have a URL.
1101
    if (!options.url) {
1102
      params.url = getUrl(model, options) || urlError();
1103
    }
1104

    
1105
    // Ensure that we have the appropriate request data.
1106
    if (!options.data && model && (method == 'create' || method == 'update')) {
1107
      params.contentType = 'application/json';
1108
      params.data = JSON.stringify(model.toJSON());
1109
    }
1110

    
1111
    // For older servers, emulate JSON by encoding the request into an HTML-form.
1112
    if (Backbone.emulateJSON) {
1113
      params.contentType = 'application/x-www-form-urlencoded';
1114
      params.data = params.data ? {model : params.data} : {};
1115
    }
1116

    
1117
    // For older servers, emulate HTTP by mimicking the HTTP method with `_method`
1118
    // And an `X-HTTP-Method-Override` header.
1119
    if (Backbone.emulateHTTP) {
1120
      if (type === 'PUT' || type === 'DELETE') {
1121
        if (Backbone.emulateJSON) params.data._method = type;
1122
        params.type = 'POST';
1123
        params.beforeSend = function(xhr) {
1124
          xhr.setRequestHeader('X-HTTP-Method-Override', type);
1125
        };
1126
      }
1127
    }
1128

    
1129
    // Don't process data on a non-GET request.
1130
    if (params.type !== 'GET' && !Backbone.emulateJSON) {
1131
      params.processData = false;
1132
    }
1133

    
1134
    // Make the request, allowing the user to override any Ajax options.
1135
    return $.ajax(_.extend(params, options));
1136
  };
1137

    
1138
  // Helpers
1139
  // -------
1140

    
1141
  // Shared empty constructor function to aid in prototype-chain creation.
1142
  var ctor = function(){};
1143

    
1144
  // Helper function to correctly set up the prototype chain, for subclasses.
1145
  // Similar to `goog.inherits`, but uses a hash of prototype properties and
1146
  // class properties to be extended.
1147
  var inherits = function(parent, protoProps, staticProps) {
1148
    var child;
1149

    
1150
    // The constructor function for the new subclass is either defined by you
1151
    // (the "constructor" property in your `extend` definition), or defaulted
1152
    // by us to simply call `super()`.
1153
    if (protoProps && protoProps.hasOwnProperty('constructor')) {
1154
      child = protoProps.constructor;
1155
    } else {
1156
      child = function(){ return parent.apply(this, arguments); };
1157
    }
1158

    
1159
    // Inherit class (static) properties from parent.
1160
    _.extend(child, parent);
1161

    
1162
    // Set the prototype chain to inherit from `parent`, without calling
1163
    // `parent`'s constructor function.
1164
    ctor.prototype = parent.prototype;
1165
    child.prototype = new ctor();
1166

    
1167
    // Add prototype properties (instance properties) to the subclass,
1168
    // if supplied.
1169
    if (protoProps) _.extend(child.prototype, protoProps);
1170

    
1171
    // Add static properties to the constructor function, if supplied.
1172
    if (staticProps) _.extend(child, staticProps);
1173

    
1174
    // Correctly set child's `prototype.constructor`.
1175
    child.prototype.constructor = child;
1176

    
1177
    // Set a convenience property in case the parent's prototype is needed later.
1178
    child.__super__ = parent.prototype;
1179

    
1180
    return child;
1181
  };
1182

    
1183
  // Helper function to get a URL from a Model or Collection as a property
1184
  // or as a function.
1185
  var getUrl = function(object, options) {
1186
    if (!(object && object.url)) return null;
1187
    return _.isFunction(object.url) ? object.url(options) : object.url;
1188
  };
1189

    
1190
  // Throw an error when a URL is needed, and none is supplied.
1191
  var urlError = function() {
1192
    throw new Error('A "url" property or function must be specified');
1193
  };
1194

    
1195
  // Wrap an optional error callback with a fallback error event.
1196
  var wrapError = function(onError, originalModel, options) {
1197
    return function(model, resp) {
1198
      var model = originalModel;
1199
      if (onError) {
1200
        onError(model, resp, options);
1201
      } else {
1202
        model.trigger('error', model, resp, options);
1203
      }
1204
    };
1205
  };
1206

    
1207
}).call(this);