@@ -0,0 +1,1149 @@
1+// Backbone.js 0.5.1
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+ // The top-level namespace. All public Backbone classes and modules will
19+ // be attached to this. Exported for both CommonJS and the browser.
20+ var Backbone;
21+ if (typeof exports !== 'undefined') {
22+ Backbone = exports;
23+ } else {
24+ Backbone = root.Backbone = {};
25+ }
26+
27+ // Current version of the library. Keep in sync with `package.json`.
28+ Backbone.VERSION = '0.5.1';
29+
30+ // Require Underscore, if we're on the server, and it's not already present.
31+ var _ = root._;
32+ if (!_ && (typeof require !== 'undefined')) _ = require('underscore')._;
33+
34+ // For Backbone's purposes, jQuery or Zepto owns the `$` variable.
35+ var $ = root.jQuery || root.Zepto;
36+
37+ // Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
38+ // to its previous owner. Returns a reference to this Backbone object.
39+ Backbone.noConflict = function() {
40+ root.Backbone = previousBackbone;
41+ return this;
42+ };
43+
44+ // Turn on `emulateHTTP` to use support legacy HTTP servers. Setting this option will
45+ // fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a
46+ // `X-Http-Method-Override` header.
47+ Backbone.emulateHTTP = false;
48+
49+ // Turn on `emulateJSON` to support legacy servers that can't deal with direct
50+ // `application/json` requests ... will encode the body as
51+ // `application/x-www-form-urlencoded` instead and will send the model in a
52+ // form param named `model`.
53+ Backbone.emulateJSON = false;
54+
55+ // Backbone.Events
56+ // -----------------
57+
58+ // A module that can be mixed in to *any object* in order to provide it with
59+ // custom events. You may `bind` or `unbind` a callback function to an event;
60+ // `trigger`-ing an event fires all callbacks in succession.
61+ //
62+ // var object = {};
63+ // _.extend(object, Backbone.Events);
64+ // object.bind('expand', function(){ alert('expanded'); });
65+ // object.trigger('expand');
66+ //
67+ Backbone.Events = {
68+
69+ // Bind an event, specified by a string name, `ev`, to a `callback` function.
70+ // Passing `"all"` will bind the callback to all events fired.
71+ bind : function(ev, callback) {
72+ var calls = this._callbacks || (this._callbacks = {});
73+ var list = calls[ev] || (calls[ev] = []);
74+ list.push(callback);
75+ return this;
76+ },
77+
78+ // Remove one or many callbacks. If `callback` is null, removes all
79+ // callbacks for the event. If `ev` is null, removes all bound callbacks
80+ // for all events.
81+ unbind : function(ev, callback) {
82+ var calls;
83+ if (!ev) {
84+ this._callbacks = {};
85+ } else if (calls = this._callbacks) {
86+ if (!callback) {
87+ calls[ev] = [];
88+ } else {
89+ var list = calls[ev];
90+ if (!list) return this;
91+ for (var i = 0, l = list.length; i < l; i++) {
92+ if (callback === list[i]) {
93+ list[i] = null;
94+ break;
95+ }
96+ }
97+ }
98+ }
99+ return this;
100+ },
101+
102+ // Trigger an event, firing all bound callbacks. Callbacks are passed the
103+ // same arguments as `trigger` is, apart from the event name.
104+ // Listening for `"all"` passes the true event name as the first argument.
105+ trigger : function(eventName) {
106+ var list, calls, ev, callback, args;
107+ var both = 2;
108+ if (!(calls = this._callbacks)) return this;
109+ while (both--) {
110+ ev = both ? eventName : 'all';
111+ if (list = calls[ev]) {
112+ for (var i = 0, l = list.length; i < l; i++) {
113+ if (!(callback = list[i])) {
114+ list.splice(i, 1); i--; l--;
115+ } else {
116+ args = both ? Array.prototype.slice.call(arguments, 1) : arguments;
117+ callback.apply(this, args);
118+ }
119+ }
120+ }
121+ }
122+ return this;
123+ }
124+
125+ };
126+
127+ // Backbone.Model
128+ // --------------
129+
130+ // Create a new model, with defined attributes. A client id (`cid`)
131+ // is automatically generated and assigned for you.
132+ Backbone.Model = function(attributes, options) {
133+ var defaults;
134+ attributes || (attributes = {});
135+ if (defaults = this.defaults) {
136+ if (_.isFunction(defaults)) defaults = defaults();
137+ attributes = _.extend({}, defaults, attributes);
138+ }
139+ this.attributes = {};
140+ this._escapedAttributes = {};
141+ this.cid = _.uniqueId('c');
142+ this.set(attributes, {silent : true});
143+ this._changed = false;
144+ this._previousAttributes = _.clone(this.attributes);
145+ if (options && options.collection) this.collection = options.collection;
146+ this.initialize(attributes, options);
147+ };
148+
149+ // Attach all inheritable methods to the Model prototype.
150+ _.extend(Backbone.Model.prototype, Backbone.Events, {
151+
152+ // A snapshot of the model's previous attributes, taken immediately
153+ // after the last `"change"` event was fired.
154+ _previousAttributes : null,
155+
156+ // Has the item been changed since the last `"change"` event?
157+ _changed : false,
158+
159+ // The default name for the JSON `id` attribute is `"id"`. MongoDB and
160+ // CouchDB users may want to set this to `"_id"`.
161+ idAttribute : 'id',
162+
163+ // Initialize is an empty function by default. Override it with your own
164+ // initialization logic.
165+ initialize : function(){},
166+
167+ // Return a copy of the model's `attributes` object.
168+ toJSON : function() {
169+ return _.clone(this.attributes);
170+ },
171+
172+ // Get the value of an attribute.
173+ get : function(attr) {
174+ return this.attributes[attr];
175+ },
176+
177+ // Get the HTML-escaped value of an attribute.
178+ escape : function(attr) {
179+ var html;
180+ if (html = this._escapedAttributes[attr]) return html;
181+ var val = this.attributes[attr];
182+ return this._escapedAttributes[attr] = escapeHTML(val == null ? '' : '' + val);
183+ },
184+
185+ // Returns `true` if the attribute contains a value that is not null
186+ // or undefined.
187+ has : function(attr) {
188+ return this.attributes[attr] != null;
189+ },
190+
191+ // Set a hash of model attributes on the object, firing `"change"` unless you
192+ // choose to silence it.
193+ set : function(attrs, options) {
194+
195+ // Extract attributes and options.
196+ options || (options = {});
197+ if (!attrs) return this;
198+ if (attrs.attributes) attrs = attrs.attributes;
199+ var now = this.attributes, escaped = this._escapedAttributes;
200+
201+ // Run validation.
202+ if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;
203+
204+ // Check for changes of `id`.
205+ if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
206+
207+ // We're about to start triggering change events.
208+ var alreadyChanging = this._changing;
209+ this._changing = true;
210+
211+ // Update attributes.
212+ for (var attr in attrs) {
213+ var val = attrs[attr];
214+ if (!_.isEqual(now[attr], val)) {
215+ now[attr] = val;
216+ delete escaped[attr];
217+ this._changed = true;
218+ if (!options.silent) this.trigger('change:' + attr, this, val, options);
219+ }
220+ }
221+
222+ // Fire the `"change"` event, if the model has been changed.
223+ if (!alreadyChanging && !options.silent && this._changed) this.change(options);
224+ this._changing = false;
225+ return this;
226+ },
227+
228+ // Remove an attribute from the model, firing `"change"` unless you choose
229+ // to silence it. `unset` is a noop if the attribute doesn't exist.
230+ unset : function(attr, options) {
231+ if (!(attr in this.attributes)) return this;
232+ options || (options = {});
233+ var value = this.attributes[attr];
234+
235+ // Run validation.
236+ var validObj = {};
237+ validObj[attr] = void 0;
238+ if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
239+
240+ // Remove the attribute.
241+ delete this.attributes[attr];
242+ delete this._escapedAttributes[attr];
243+ if (attr == this.idAttribute) delete this.id;
244+ this._changed = true;
245+ if (!options.silent) {
246+ this.trigger('change:' + attr, this, void 0, options);
247+ this.change(options);
248+ }
249+ return this;
250+ },
251+
252+ // Clear all attributes on the model, firing `"change"` unless you choose
253+ // to silence it.
254+ clear : function(options) {
255+ options || (options = {});
256+ var attr;
257+ var old = this.attributes;
258+
259+ // Run validation.
260+ var validObj = {};
261+ for (attr in old) validObj[attr] = void 0;
262+ if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
263+
264+ this.attributes = {};
265+ this._escapedAttributes = {};
266+ this._changed = true;
267+ if (!options.silent) {
268+ for (attr in old) {
269+ this.trigger('change:' + attr, this, void 0, options);
270+ }
271+ this.change(options);
272+ }
273+ return this;
274+ },
275+
276+ // Fetch the model from the server. If the server's representation of the
277+ // model differs from its current attributes, they will be overriden,
278+ // triggering a `"change"` event.
279+ fetch : function(options) {
280+ options || (options = {});
281+ var model = this;
282+ var success = options.success;
283+ options.success = function(resp, status, xhr) {
284+ if (!model.set(model.parse(resp, xhr), options)) return false;
285+ if (success) success(model, resp);
286+ };
287+ options.error = wrapError(options.error, model, options);
288+ return (this.sync || Backbone.sync).call(this, 'read', this, options);
289+ },
290+
291+ // Set a hash of model attributes, and sync the model to the server.
292+ // If the server returns an attributes hash that differs, the model's
293+ // state will be `set` again.
294+ save : function(attrs, options) {
295+ options || (options = {});
296+ if (attrs && !this.set(attrs, options)) return false;
297+ var model = this;
298+ var success = options.success;
299+ options.success = function(resp, status, xhr) {
300+ if (!model.set(model.parse(resp, xhr), options)) return false;
301+ if (success) success(model, resp, xhr);
302+ };
303+ options.error = wrapError(options.error, model, options);
304+ var method = this.isNew() ? 'create' : 'update';
305+ return (this.sync || Backbone.sync).call(this, method, this, options);
306+ },
307+
308+ // Destroy this model on the server if it was already persisted. Upon success, the model is removed
309+ // from its collection, if it has one.
310+ destroy : function(options) {
311+ options || (options = {});
312+ if (this.isNew()) return this.trigger('destroy', this, this.collection, options);
313+ var model = this;
314+ var success = options.success;
315+ options.success = function(resp) {
316+ model.trigger('destroy', model, model.collection, options);
317+ if (success) success(model, resp);
318+ };
319+ options.error = wrapError(options.error, model, options);
320+ return (this.sync || Backbone.sync).call(this, 'delete', this, options);
321+ },
322+
323+ // Default URL for the model's representation on the server -- if you're
324+ // using Backbone's restful methods, override this to change the endpoint
325+ // that will be called.
326+ url : function() {
327+ var base = getUrl(this.collection) || this.urlRoot || urlError();
328+ if (this.isNew()) return base;
329+ return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
330+ },
331+
332+ // **parse** converts a response into the hash of attributes to be `set` on
333+ // the model. The default implementation is just to pass the response along.
334+ parse : function(resp, xhr) {
335+ return resp;
336+ },
337+
338+ // Create a new model with identical attributes to this one.
339+ clone : function() {
340+ return new this.constructor(this);
341+ },
342+
343+ // A model is new if it has never been saved to the server, and lacks an id.
344+ isNew : function() {
345+ return this.id == null;
346+ },
347+
348+ // Call this method to manually fire a `change` event for this model.
349+ // Calling this will cause all objects observing the model to update.
350+ change : function(options) {
351+ this.trigger('change', this, options);
352+ this._previousAttributes = _.clone(this.attributes);
353+ this._changed = false;
354+ },
355+
356+ // Determine if the model has changed since the last `"change"` event.
357+ // If you specify an attribute name, determine if that attribute has changed.
358+ hasChanged : function(attr) {
359+ if (attr) return this._previousAttributes[attr] != this.attributes[attr];
360+ return this._changed;
361+ },
362+
363+ // Return an object containing all the attributes that have changed, or false
364+ // if there are no changed attributes. Useful for determining what parts of a
365+ // view need to be updated and/or what attributes need to be persisted to
366+ // the server.
367+ changedAttributes : function(now) {
368+ now || (now = this.attributes);
369+ var old = this._previousAttributes;
370+ var changed = false;
371+ for (var attr in now) {
372+ if (!_.isEqual(old[attr], now[attr])) {
373+ changed = changed || {};
374+ changed[attr] = now[attr];
375+ }
376+ }
Cut short: this commit's diff is over 1000 lines.