Statistics
| Branch: | Tag: | Revision:

root / snf-app / synnefo / ui / static / snf / js / lib / backbone.js @ 483c9197

History | View | Annotate | Download (43.6 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
        model.trigger('destroy', model, model.collection, options);
311
        if (success) success(model, resp);
312
      };
313
      options.error = wrapError(options.error, model, options);
314
      return (this.sync || Backbone.sync).call(this, 'delete', this, options);
315
    },
316

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

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

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

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

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

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

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

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

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

    
380
      return changed;
381
    },
382

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

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

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

    
412
  });
413

    
414
  // Backbone.Collection
415
  // -------------------
416

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
530
      _.each( models, function(model) {
531

    
532
        var idAttribute = this.model.prototype.idAttribute;
533
        var modelId = model[idAttribute];
534

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

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

    
554
      return this;
555
    },
556

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

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

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

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

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

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

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

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

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

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

    
685
  });
686

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

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

    
700
  // Backbone.Router
701
  // -------------------
702

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

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

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

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

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

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

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

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

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

    
775
  });
776

    
777
  // Backbone.History
778
  // ----------------
779

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

    
787
  // Cached regex for cleaning hashes.
788
  var hashStrip = /^#*/;
789

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

    
793
  // Has the history handling already been started?
794
  var historyStarted = false;
795

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

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

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

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

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

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

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

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

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

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

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

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

    
920
  });
921

    
922
  // Backbone.View
923
  // -------------
924

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

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

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

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

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

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

    
954
    // Attach the `selectorDelegate` function as the `$` property.
955
    $       : selectorDelegate,
956

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

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

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

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

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

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

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

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

    
1052
  });
1053

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

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

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

    
1073
  // Backbone.sync
1074
  // -------------
1075

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

    
1094
    // Default JSON-request options.
1095
    var params = {type : type, dataType : 'json'};
1096

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

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

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

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

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

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

    
1135
  // Helpers
1136
  // -------
1137

    
1138
  // Shared empty constructor function to aid in prototype-chain creation.
1139
  var ctor = function(){};
1140

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

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

    
1156
    // Inherit class (static) properties from parent.
1157
    _.extend(child, parent);
1158

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

    
1164
    // Add prototype properties (instance properties) to the subclass,
1165
    // if supplied.
1166
    if (protoProps) _.extend(child.prototype, protoProps);
1167

    
1168
    // Add static properties to the constructor function, if supplied.
1169
    if (staticProps) _.extend(child, staticProps);
1170

    
1171
    // Correctly set child's `prototype.constructor`.
1172
    child.prototype.constructor = child;
1173

    
1174
    // Set a convenience property in case the parent's prototype is needed later.
1175
    child.__super__ = parent.prototype;
1176

    
1177
    return child;
1178
  };
1179

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

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

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

    
1204
}).call(this);