[IMP] update backbone
bzr revid: al@openerp.com-20120813170739-kxm4s289mbm2tty4
This commit is contained in:
parent
3e797902fc
commit
5fe34edd38
|
@ -1,4 +1,4 @@
|
||||||
// Backbone.js 0.9.1
|
// Backbone.js 0.9.2
|
||||||
|
|
||||||
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
||||||
// Backbone may be freely distributed under the MIT license.
|
// Backbone may be freely distributed under the MIT license.
|
||||||
|
@ -32,7 +32,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current version of the library. Keep in sync with `package.json`.
|
// Current version of the library. Keep in sync with `package.json`.
|
||||||
Backbone.VERSION = '0.9.1';
|
Backbone.VERSION = '0.9.2';
|
||||||
|
|
||||||
// Require Underscore, if we're on the server, and it's not already present.
|
// Require Underscore, if we're on the server, and it's not already present.
|
||||||
var _ = root._;
|
var _ = root._;
|
||||||
|
@ -71,6 +71,9 @@
|
||||||
// Backbone.Events
|
// Backbone.Events
|
||||||
// -----------------
|
// -----------------
|
||||||
|
|
||||||
|
// Regular expression used to split event strings
|
||||||
|
var eventSplitter = /\s+/;
|
||||||
|
|
||||||
// A module that can be mixed in to *any object* in order to provide it with
|
// A module that can be mixed in to *any object* in order to provide it with
|
||||||
// custom events. You may bind with `on` or remove with `off` callback functions
|
// custom events. You may bind with `on` or remove with `off` callback functions
|
||||||
// to an event; trigger`-ing an event fires all callbacks in succession.
|
// to an event; trigger`-ing an event fires all callbacks in succession.
|
||||||
|
@ -80,89 +83,110 @@
|
||||||
// object.on('expand', function(){ alert('expanded'); });
|
// object.on('expand', function(){ alert('expanded'); });
|
||||||
// object.trigger('expand');
|
// object.trigger('expand');
|
||||||
//
|
//
|
||||||
Backbone.Events = {
|
var Events = Backbone.Events = {
|
||||||
|
|
||||||
// Bind an event, specified by a string name, `ev`, to a `callback`
|
// Bind one or more space separated events, `events`, to a `callback`
|
||||||
// function. Passing `"all"` will bind the callback to all events fired.
|
// function. Passing `"all"` will bind the callback to all events fired.
|
||||||
on: function(events, callback, context) {
|
on: function(events, callback, context) {
|
||||||
var ev;
|
|
||||||
events = events.split(/\s+/);
|
var calls, event, node, tail, list;
|
||||||
var calls = this._callbacks || (this._callbacks = {});
|
if (!callback) return this;
|
||||||
while (ev = events.shift()) {
|
events = events.split(eventSplitter);
|
||||||
// Create an immutable callback list, allowing traversal during
|
calls = this._callbacks || (this._callbacks = {});
|
||||||
// modification. The tail is an empty object that will always be used
|
|
||||||
// as the next node.
|
// Create an immutable callback list, allowing traversal during
|
||||||
var list = calls[ev] || (calls[ev] = {});
|
// modification. The tail is an empty object that will always be used
|
||||||
var tail = list.tail || (list.tail = list.next = {});
|
// as the next node.
|
||||||
tail.callback = callback;
|
while (event = events.shift()) {
|
||||||
tail.context = context;
|
list = calls[event];
|
||||||
list.tail = tail.next = {};
|
node = list ? list.tail : {};
|
||||||
|
node.next = tail = {};
|
||||||
|
node.context = context;
|
||||||
|
node.callback = callback;
|
||||||
|
calls[event] = {tail: tail, next: list ? list.next : node};
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Remove one or many callbacks. If `context` is null, removes all callbacks
|
// Remove one or many callbacks. If `context` is null, removes all callbacks
|
||||||
// with that function. If `callback` is null, removes all callbacks for the
|
// with that function. If `callback` is null, removes all callbacks for the
|
||||||
// event. If `ev` is null, removes all bound callbacks for all events.
|
// event. If `events` is null, removes all bound callbacks for all events.
|
||||||
off: function(events, callback, context) {
|
off: function(events, callback, context) {
|
||||||
var ev, calls, node;
|
var event, calls, node, tail, cb, ctx;
|
||||||
if (!events) {
|
|
||||||
|
// No events, or removing *all* events.
|
||||||
|
if (!(calls = this._callbacks)) return;
|
||||||
|
if (!(events || callback || context)) {
|
||||||
delete this._callbacks;
|
delete this._callbacks;
|
||||||
} else if (calls = this._callbacks) {
|
return this;
|
||||||
events = events.split(/\s+/);
|
}
|
||||||
while (ev = events.shift()) {
|
|
||||||
node = calls[ev];
|
// Loop through the listed events and contexts, splicing them out of the
|
||||||
delete calls[ev];
|
// linked list of callbacks if appropriate.
|
||||||
if (!callback || !node) continue;
|
events = events ? events.split(eventSplitter) : _.keys(calls);
|
||||||
// Create a new list, omitting the indicated event/context pairs.
|
while (event = events.shift()) {
|
||||||
while ((node = node.next) && node.next) {
|
node = calls[event];
|
||||||
if (node.callback === callback &&
|
delete calls[event];
|
||||||
(!context || node.context === context)) continue;
|
if (!node || !(callback || context)) continue;
|
||||||
this.on(ev, node.callback, node.context);
|
// Create a new list, omitting the indicated callbacks.
|
||||||
|
tail = node.tail;
|
||||||
|
while ((node = node.next) !== tail) {
|
||||||
|
cb = node.callback;
|
||||||
|
ctx = node.context;
|
||||||
|
if ((callback && cb !== callback) || (context && ctx !== context)) {
|
||||||
|
this.on(event, cb, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Trigger an event, firing all bound callbacks. Callbacks are passed the
|
// Trigger one or many events, firing all bound callbacks. Callbacks are
|
||||||
// same arguments as `trigger` is, apart from the event name.
|
// passed the same arguments as `trigger` is, apart from the event name
|
||||||
// Listening for `"all"` passes the true event name as the first argument.
|
// (unless you're listening on `"all"`, which will cause your callback to
|
||||||
|
// receive the true name of the event as the first argument).
|
||||||
trigger: function(events) {
|
trigger: function(events) {
|
||||||
var event, node, calls, tail, args, all, rest;
|
var event, node, calls, tail, args, all, rest;
|
||||||
if (!(calls = this._callbacks)) return this;
|
if (!(calls = this._callbacks)) return this;
|
||||||
all = calls['all'];
|
all = calls.all;
|
||||||
(events = events.split(/\s+/)).push(null);
|
events = events.split(eventSplitter);
|
||||||
// Save references to the current heads & tails.
|
|
||||||
while (event = events.shift()) {
|
|
||||||
if (all) events.push({next: all.next, tail: all.tail, event: event});
|
|
||||||
if (!(node = calls[event])) continue;
|
|
||||||
events.push({next: node.next, tail: node.tail});
|
|
||||||
}
|
|
||||||
// Traverse each list, stopping when the saved tail is reached.
|
|
||||||
rest = slice.call(arguments, 1);
|
rest = slice.call(arguments, 1);
|
||||||
while (node = events.pop()) {
|
|
||||||
tail = node.tail;
|
// For each event, walk through the linked list of callbacks twice,
|
||||||
args = node.event ? [node.event].concat(rest) : rest;
|
// first to trigger the event, then to trigger any `"all"` callbacks.
|
||||||
while ((node = node.next) !== tail) {
|
while (event = events.shift()) {
|
||||||
node.callback.apply(node.context || this, args);
|
if (node = calls[event]) {
|
||||||
|
tail = node.tail;
|
||||||
|
while ((node = node.next) !== tail) {
|
||||||
|
node.callback.apply(node.context || this, rest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (node = all) {
|
||||||
|
tail = node.tail;
|
||||||
|
args = [event].concat(rest);
|
||||||
|
while ((node = node.next) !== tail) {
|
||||||
|
node.callback.apply(node.context || this, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Aliases for backwards compatibility.
|
// Aliases for backwards compatibility.
|
||||||
Backbone.Events.bind = Backbone.Events.on;
|
Events.bind = Events.on;
|
||||||
Backbone.Events.unbind = Backbone.Events.off;
|
Events.unbind = Events.off;
|
||||||
|
|
||||||
// Backbone.Model
|
// Backbone.Model
|
||||||
// --------------
|
// --------------
|
||||||
|
|
||||||
// Create a new model, with defined attributes. A client id (`cid`)
|
// Create a new model, with defined attributes. A client id (`cid`)
|
||||||
// is automatically generated and assigned for you.
|
// is automatically generated and assigned for you.
|
||||||
Backbone.Model = function(attributes, options) {
|
var Model = Backbone.Model = function(attributes, options) {
|
||||||
var defaults;
|
var defaults;
|
||||||
attributes || (attributes = {});
|
attributes || (attributes = {});
|
||||||
if (options && options.parse) attributes = this.parse(attributes);
|
if (options && options.parse) attributes = this.parse(attributes);
|
||||||
|
@ -173,16 +197,31 @@
|
||||||
this.attributes = {};
|
this.attributes = {};
|
||||||
this._escapedAttributes = {};
|
this._escapedAttributes = {};
|
||||||
this.cid = _.uniqueId('c');
|
this.cid = _.uniqueId('c');
|
||||||
if (!this.set(attributes, {silent: true})) {
|
this.changed = {};
|
||||||
throw new Error("Can't create an invalid model");
|
this._silent = {};
|
||||||
}
|
this._pending = {};
|
||||||
delete this._changed;
|
this.set(attributes, {silent: true});
|
||||||
|
// Reset change tracking.
|
||||||
|
this.changed = {};
|
||||||
|
this._silent = {};
|
||||||
|
this._pending = {};
|
||||||
this._previousAttributes = _.clone(this.attributes);
|
this._previousAttributes = _.clone(this.attributes);
|
||||||
this.initialize.apply(this, arguments);
|
this.initialize.apply(this, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attach all inheritable methods to the Model prototype.
|
// Attach all inheritable methods to the Model prototype.
|
||||||
_.extend(Backbone.Model.prototype, Backbone.Events, {
|
_.extend(Model.prototype, Events, {
|
||||||
|
|
||||||
|
// A hash of attributes whose current and previous value differ.
|
||||||
|
changed: null,
|
||||||
|
|
||||||
|
// A hash of attributes that have silently changed since the last time
|
||||||
|
// `change` was called. Will become pending attributes on the next call.
|
||||||
|
_silent: null,
|
||||||
|
|
||||||
|
// A hash of attributes that have changed since the last `'change'` event
|
||||||
|
// began.
|
||||||
|
_pending: null,
|
||||||
|
|
||||||
// The default name for the JSON `id` attribute is `"id"`. MongoDB and
|
// The default name for the JSON `id` attribute is `"id"`. MongoDB and
|
||||||
// CouchDB users may want to set this to `"_id"`.
|
// CouchDB users may want to set this to `"_id"`.
|
||||||
|
@ -193,7 +232,7 @@
|
||||||
initialize: function(){},
|
initialize: function(){},
|
||||||
|
|
||||||
// Return a copy of the model's `attributes` object.
|
// Return a copy of the model's `attributes` object.
|
||||||
toJSON: function() {
|
toJSON: function(options) {
|
||||||
return _.clone(this.attributes);
|
return _.clone(this.attributes);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -206,20 +245,22 @@
|
||||||
escape: function(attr) {
|
escape: function(attr) {
|
||||||
var html;
|
var html;
|
||||||
if (html = this._escapedAttributes[attr]) return html;
|
if (html = this._escapedAttributes[attr]) return html;
|
||||||
var val = this.attributes[attr];
|
var val = this.get(attr);
|
||||||
return this._escapedAttributes[attr] = _.escape(val == null ? '' : '' + val);
|
return this._escapedAttributes[attr] = _.escape(val == null ? '' : '' + val);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Returns `true` if the attribute contains a value that is not null
|
// Returns `true` if the attribute contains a value that is not null
|
||||||
// or undefined.
|
// or undefined.
|
||||||
has: function(attr) {
|
has: function(attr) {
|
||||||
return this.attributes[attr] != null;
|
return this.get(attr) != null;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Set a hash of model attributes on the object, firing `"change"` unless
|
// Set a hash of model attributes on the object, firing `"change"` unless
|
||||||
// you choose to silence it.
|
// you choose to silence it.
|
||||||
set: function(key, value, options) {
|
set: function(key, value, options) {
|
||||||
var attrs, attr, val;
|
var attrs, attr, val;
|
||||||
|
|
||||||
|
// Handle both `"key", value` and `{key: value}` -style arguments.
|
||||||
if (_.isObject(key) || key == null) {
|
if (_.isObject(key) || key == null) {
|
||||||
attrs = key;
|
attrs = key;
|
||||||
options = value;
|
options = value;
|
||||||
|
@ -231,7 +272,7 @@
|
||||||
// Extract attributes and options.
|
// Extract attributes and options.
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
if (!attrs) return this;
|
if (!attrs) return this;
|
||||||
if (attrs instanceof Backbone.Model) attrs = attrs.attributes;
|
if (attrs instanceof Model) attrs = attrs.attributes;
|
||||||
if (options.unset) for (attr in attrs) attrs[attr] = void 0;
|
if (options.unset) for (attr in attrs) attrs[attr] = void 0;
|
||||||
|
|
||||||
// Run validation.
|
// Run validation.
|
||||||
|
@ -240,33 +281,37 @@
|
||||||
// Check for changes of `id`.
|
// Check for changes of `id`.
|
||||||
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
|
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
|
||||||
|
|
||||||
|
var changes = options.changes = {};
|
||||||
var now = this.attributes;
|
var now = this.attributes;
|
||||||
var escaped = this._escapedAttributes;
|
var escaped = this._escapedAttributes;
|
||||||
var prev = this._previousAttributes || {};
|
var prev = this._previousAttributes || {};
|
||||||
var alreadySetting = this._setting;
|
|
||||||
this._changed || (this._changed = {});
|
|
||||||
this._setting = true;
|
|
||||||
|
|
||||||
// Update attributes.
|
// For each `set` attribute...
|
||||||
for (attr in attrs) {
|
for (attr in attrs) {
|
||||||
val = attrs[attr];
|
val = attrs[attr];
|
||||||
if (!_.isEqual(now[attr], val)) delete escaped[attr];
|
|
||||||
options.unset ? delete now[attr] : now[attr] = val;
|
// If the new and current value differ, record the change.
|
||||||
if (this._changing && !_.isEqual(this._changed[attr], val)) {
|
if (!_.isEqual(now[attr], val) || (options.unset && _.has(now, attr))) {
|
||||||
this.trigger('change:' + attr, this, val, options);
|
delete escaped[attr];
|
||||||
this._moreChanges = true;
|
(options.silent ? this._silent : changes)[attr] = true;
|
||||||
}
|
}
|
||||||
delete this._changed[attr];
|
|
||||||
|
// Update or delete the current value.
|
||||||
|
options.unset ? delete now[attr] : now[attr] = val;
|
||||||
|
|
||||||
|
// If the new and previous value differ, record the change. If not,
|
||||||
|
// then remove changes for this attribute.
|
||||||
if (!_.isEqual(prev[attr], val) || (_.has(now, attr) != _.has(prev, attr))) {
|
if (!_.isEqual(prev[attr], val) || (_.has(now, attr) != _.has(prev, attr))) {
|
||||||
this._changed[attr] = val;
|
this.changed[attr] = val;
|
||||||
|
if (!options.silent) this._pending[attr] = true;
|
||||||
|
} else {
|
||||||
|
delete this.changed[attr];
|
||||||
|
delete this._pending[attr];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fire the `"change"` events, if the model has been changed.
|
// Fire the `"change"` events.
|
||||||
if (!alreadySetting) {
|
if (!options.silent) this.change(options);
|
||||||
if (!options.silent && this.hasChanged()) this.change(options);
|
|
||||||
this._setting = false;
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -304,6 +349,8 @@
|
||||||
// state will be `set` again.
|
// state will be `set` again.
|
||||||
save: function(key, value, options) {
|
save: function(key, value, options) {
|
||||||
var attrs, current;
|
var attrs, current;
|
||||||
|
|
||||||
|
// Handle both `("key", value)` and `({key: value})` -style calls.
|
||||||
if (_.isObject(key) || key == null) {
|
if (_.isObject(key) || key == null) {
|
||||||
attrs = key;
|
attrs = key;
|
||||||
options = value;
|
options = value;
|
||||||
|
@ -311,18 +358,30 @@
|
||||||
attrs = {};
|
attrs = {};
|
||||||
attrs[key] = value;
|
attrs[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
options = options ? _.clone(options) : {};
|
options = options ? _.clone(options) : {};
|
||||||
if (options.wait) current = _.clone(this.attributes);
|
|
||||||
|
// If we're "wait"-ing to set changed attributes, validate early.
|
||||||
|
if (options.wait) {
|
||||||
|
if (!this._validate(attrs, options)) return false;
|
||||||
|
current = _.clone(this.attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regular saves `set` attributes before persisting to the server.
|
||||||
var silentOptions = _.extend({}, options, {silent: true});
|
var silentOptions = _.extend({}, options, {silent: true});
|
||||||
if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
|
if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// After a successful server-side save, the client is (optionally)
|
||||||
|
// updated with the server-side state.
|
||||||
var model = this;
|
var model = this;
|
||||||
var success = options.success;
|
var success = options.success;
|
||||||
options.success = function(resp, status, xhr) {
|
options.success = function(resp, status, xhr) {
|
||||||
var serverAttrs = model.parse(resp, xhr);
|
var serverAttrs = model.parse(resp, xhr);
|
||||||
if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
|
if (options.wait) {
|
||||||
|
delete options.wait;
|
||||||
|
serverAttrs = _.extend(attrs || {}, serverAttrs);
|
||||||
|
}
|
||||||
if (!model.set(serverAttrs, options)) return false;
|
if (!model.set(serverAttrs, options)) return false;
|
||||||
if (success) {
|
if (success) {
|
||||||
success(model, resp);
|
success(model, resp);
|
||||||
|
@ -330,6 +389,8 @@
|
||||||
model.trigger('sync', model, resp, options);
|
model.trigger('sync', model, resp, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Finish configuring and sending the Ajax request.
|
||||||
options.error = Backbone.wrapError(options.error, model, options);
|
options.error = Backbone.wrapError(options.error, model, options);
|
||||||
var method = this.isNew() ? 'create' : 'update';
|
var method = this.isNew() ? 'create' : 'update';
|
||||||
var xhr = (this.sync || Backbone.sync).call(this, method, this, options);
|
var xhr = (this.sync || Backbone.sync).call(this, method, this, options);
|
||||||
|
@ -349,7 +410,11 @@
|
||||||
model.trigger('destroy', model, model.collection, options);
|
model.trigger('destroy', model, model.collection, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.isNew()) return triggerDestroy();
|
if (this.isNew()) {
|
||||||
|
triggerDestroy();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
options.success = function(resp) {
|
options.success = function(resp) {
|
||||||
if (options.wait) triggerDestroy();
|
if (options.wait) triggerDestroy();
|
||||||
if (success) {
|
if (success) {
|
||||||
|
@ -358,6 +423,7 @@
|
||||||
model.trigger('sync', model, resp, options);
|
model.trigger('sync', model, resp, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
options.error = Backbone.wrapError(options.error, model, options);
|
options.error = Backbone.wrapError(options.error, model, options);
|
||||||
var xhr = (this.sync || Backbone.sync).call(this, 'delete', this, options);
|
var xhr = (this.sync || Backbone.sync).call(this, 'delete', this, options);
|
||||||
if (!options.wait) triggerDestroy();
|
if (!options.wait) triggerDestroy();
|
||||||
|
@ -368,7 +434,7 @@
|
||||||
// using Backbone's restful methods, override this to change the endpoint
|
// using Backbone's restful methods, override this to change the endpoint
|
||||||
// that will be called.
|
// that will be called.
|
||||||
url: function() {
|
url: function() {
|
||||||
var base = getValue(this.collection, 'url') || getValue(this, 'urlRoot') || urlError();
|
var base = getValue(this, 'urlRoot') || getValue(this.collection, 'url') || urlError();
|
||||||
if (this.isNew()) return base;
|
if (this.isNew()) return base;
|
||||||
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
|
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
|
||||||
},
|
},
|
||||||
|
@ -393,18 +459,33 @@
|
||||||
// a `"change:attribute"` event for each changed attribute.
|
// a `"change:attribute"` event for each changed attribute.
|
||||||
// Calling this will cause all objects observing the model to update.
|
// Calling this will cause all objects observing the model to update.
|
||||||
change: function(options) {
|
change: function(options) {
|
||||||
if (this._changing || !this.hasChanged()) return this;
|
options || (options = {});
|
||||||
|
var changing = this._changing;
|
||||||
this._changing = true;
|
this._changing = true;
|
||||||
this._moreChanges = true;
|
|
||||||
for (var attr in this._changed) {
|
// Silent changes become pending changes.
|
||||||
this.trigger('change:' + attr, this, this._changed[attr], options);
|
for (var attr in this._silent) this._pending[attr] = true;
|
||||||
|
|
||||||
|
// Silent changes are triggered.
|
||||||
|
var changes = _.extend({}, options.changes, this._silent);
|
||||||
|
this._silent = {};
|
||||||
|
for (var attr in changes) {
|
||||||
|
this.trigger('change:' + attr, this, this.get(attr), options);
|
||||||
}
|
}
|
||||||
while (this._moreChanges) {
|
if (changing) return this;
|
||||||
this._moreChanges = false;
|
|
||||||
|
// Continue firing `"change"` events while there are pending changes.
|
||||||
|
while (!_.isEmpty(this._pending)) {
|
||||||
|
this._pending = {};
|
||||||
this.trigger('change', this, options);
|
this.trigger('change', this, options);
|
||||||
|
// Pending and silent changes still remain.
|
||||||
|
for (var attr in this.changed) {
|
||||||
|
if (this._pending[attr] || this._silent[attr]) continue;
|
||||||
|
delete this.changed[attr];
|
||||||
|
}
|
||||||
|
this._previousAttributes = _.clone(this.attributes);
|
||||||
}
|
}
|
||||||
this._previousAttributes = _.clone(this.attributes);
|
|
||||||
delete this._changed;
|
|
||||||
this._changing = false;
|
this._changing = false;
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
@ -412,8 +493,8 @@
|
||||||
// Determine if the model has changed since the last `"change"` event.
|
// Determine if the model has changed since the last `"change"` event.
|
||||||
// If you specify an attribute name, determine if that attribute has changed.
|
// If you specify an attribute name, determine if that attribute has changed.
|
||||||
hasChanged: function(attr) {
|
hasChanged: function(attr) {
|
||||||
if (!arguments.length) return !_.isEmpty(this._changed);
|
if (!arguments.length) return !_.isEmpty(this.changed);
|
||||||
return this._changed && _.has(this._changed, attr);
|
return _.has(this.changed, attr);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Return an object containing all the attributes that have changed, or
|
// Return an object containing all the attributes that have changed, or
|
||||||
|
@ -423,7 +504,7 @@
|
||||||
// You can also pass an attributes object to diff against the model,
|
// You can also pass an attributes object to diff against the model,
|
||||||
// determining if there *would be* a change.
|
// determining if there *would be* a change.
|
||||||
changedAttributes: function(diff) {
|
changedAttributes: function(diff) {
|
||||||
if (!diff) return this.hasChanged() ? _.clone(this._changed) : false;
|
if (!diff) return this.hasChanged() ? _.clone(this.changed) : false;
|
||||||
var val, changed = false, old = this._previousAttributes;
|
var val, changed = false, old = this._previousAttributes;
|
||||||
for (var attr in diff) {
|
for (var attr in diff) {
|
||||||
if (_.isEqual(old[attr], (val = diff[attr]))) continue;
|
if (_.isEqual(old[attr], (val = diff[attr]))) continue;
|
||||||
|
@ -451,9 +532,9 @@
|
||||||
return !this.validate(this.attributes);
|
return !this.validate(this.attributes);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Run validation against a set of incoming attributes, returning `true`
|
// Run validation against the next complete set of model attributes,
|
||||||
// if all is well. If a specific `error` callback has been passed,
|
// returning `true` if all is well. If a specific `error` callback has
|
||||||
// call that instead of firing the general `"error"` event.
|
// been passed, call that instead of firing the general `"error"` event.
|
||||||
_validate: function(attrs, options) {
|
_validate: function(attrs, options) {
|
||||||
if (options.silent || !this.validate) return true;
|
if (options.silent || !this.validate) return true;
|
||||||
attrs = _.extend({}, this.attributes, attrs);
|
attrs = _.extend({}, this.attributes, attrs);
|
||||||
|
@ -475,8 +556,9 @@
|
||||||
// Provides a standard collection class for our sets of models, ordered
|
// Provides a standard collection class for our sets of models, ordered
|
||||||
// or unordered. If a `comparator` is specified, the Collection will maintain
|
// or unordered. If a `comparator` is specified, the Collection will maintain
|
||||||
// its models in sort order, as they're added and removed.
|
// its models in sort order, as they're added and removed.
|
||||||
Backbone.Collection = function(models, options) {
|
var Collection = Backbone.Collection = function(models, options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
|
if (options.model) this.model = options.model;
|
||||||
if (options.comparator) this.comparator = options.comparator;
|
if (options.comparator) this.comparator = options.comparator;
|
||||||
this._reset();
|
this._reset();
|
||||||
this.initialize.apply(this, arguments);
|
this.initialize.apply(this, arguments);
|
||||||
|
@ -484,11 +566,11 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
// Define the Collection's inheritable methods.
|
// Define the Collection's inheritable methods.
|
||||||
_.extend(Backbone.Collection.prototype, Backbone.Events, {
|
_.extend(Collection.prototype, Events, {
|
||||||
|
|
||||||
// The default model for a collection is just a **Backbone.Model**.
|
// The default model for a collection is just a **Backbone.Model**.
|
||||||
// This should be overridden in most cases.
|
// This should be overridden in most cases.
|
||||||
model: Backbone.Model,
|
model: Model,
|
||||||
|
|
||||||
// Initialize is an empty function by default. Override it with your own
|
// Initialize is an empty function by default. Override it with your own
|
||||||
// initialization logic.
|
// initialization logic.
|
||||||
|
@ -496,14 +578,14 @@
|
||||||
|
|
||||||
// The JSON representation of a Collection is an array of the
|
// The JSON representation of a Collection is an array of the
|
||||||
// models' attributes.
|
// models' attributes.
|
||||||
toJSON: function() {
|
toJSON: function(options) {
|
||||||
return this.map(function(model){ return model.toJSON(); });
|
return this.map(function(model){ return model.toJSON(options); });
|
||||||
},
|
},
|
||||||
|
|
||||||
// Add a model, or list of models to the set. Pass **silent** to avoid
|
// Add a model, or list of models to the set. Pass **silent** to avoid
|
||||||
// firing the `add` event for every new model.
|
// firing the `add` event for every new model.
|
||||||
add: function(models, options) {
|
add: function(models, options) {
|
||||||
var i, index, length, model, cid, id, cids = {}, ids = {};
|
var i, index, length, model, cid, id, cids = {}, ids = {}, dups = [];
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
models = _.isArray(models) ? models.slice() : [models];
|
models = _.isArray(models) ? models.slice() : [models];
|
||||||
|
|
||||||
|
@ -513,16 +595,24 @@
|
||||||
if (!(model = models[i] = this._prepareModel(models[i], options))) {
|
if (!(model = models[i] = this._prepareModel(models[i], options))) {
|
||||||
throw new Error("Can't add an invalid model to a collection");
|
throw new Error("Can't add an invalid model to a collection");
|
||||||
}
|
}
|
||||||
if (cids[cid = model.cid] || this._byCid[cid] ||
|
cid = model.cid;
|
||||||
(((id = model.id) != null) && (ids[id] || this._byId[id]))) {
|
id = model.id;
|
||||||
throw new Error("Can't add the same model to a collection twice");
|
if (cids[cid] || this._byCid[cid] || ((id != null) && (ids[id] || this._byId[id]))) {
|
||||||
|
dups.push(i);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
cids[cid] = ids[id] = model;
|
cids[cid] = ids[id] = model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove duplicates.
|
||||||
|
i = dups.length;
|
||||||
|
while (i--) {
|
||||||
|
models.splice(dups[i], 1);
|
||||||
|
}
|
||||||
|
|
||||||
// Listen to added models' events, and index models for lookup by
|
// Listen to added models' events, and index models for lookup by
|
||||||
// `id` and by `cid`.
|
// `id` and by `cid`.
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0, length = models.length; i < length; i++) {
|
||||||
(model = models[i]).on('all', this._onModelEvent, this);
|
(model = models[i]).on('all', this._onModelEvent, this);
|
||||||
this._byCid[model.cid] = model;
|
this._byCid[model.cid] = model;
|
||||||
if (model.id != null) this._byId[model.id] = model;
|
if (model.id != null) this._byId[model.id] = model;
|
||||||
|
@ -566,9 +656,37 @@
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Add a model to the end of the collection.
|
||||||
|
push: function(model, options) {
|
||||||
|
model = this._prepareModel(model, options);
|
||||||
|
this.add(model, options);
|
||||||
|
return model;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Remove a model from the end of the collection.
|
||||||
|
pop: function(options) {
|
||||||
|
var model = this.at(this.length - 1);
|
||||||
|
this.remove(model, options);
|
||||||
|
return model;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Add a model to the beginning of the collection.
|
||||||
|
unshift: function(model, options) {
|
||||||
|
model = this._prepareModel(model, options);
|
||||||
|
this.add(model, _.extend({at: 0}, options));
|
||||||
|
return model;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Remove a model from the beginning of the collection.
|
||||||
|
shift: function(options) {
|
||||||
|
var model = this.at(0);
|
||||||
|
this.remove(model, options);
|
||||||
|
return model;
|
||||||
|
},
|
||||||
|
|
||||||
// Get a model from the set by id.
|
// Get a model from the set by id.
|
||||||
get: function(id) {
|
get: function(id) {
|
||||||
if (id == null) return null;
|
if (id == null) return void 0;
|
||||||
return this._byId[id.id != null ? id.id : id];
|
return this._byId[id.id != null ? id.id : id];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -582,6 +700,17 @@
|
||||||
return this.models[index];
|
return this.models[index];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Return models with matching attributes. Useful for simple cases of `filter`.
|
||||||
|
where: function(attrs) {
|
||||||
|
if (_.isEmpty(attrs)) return [];
|
||||||
|
return this.filter(function(model) {
|
||||||
|
for (var key in attrs) {
|
||||||
|
if (attrs[key] !== model.get(key)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
// Force the collection to re-sort itself. You don't need to call this under
|
// Force the collection to re-sort itself. You don't need to call this under
|
||||||
// normal circumstances, as the set will maintain sort order as each item
|
// normal circumstances, as the set will maintain sort order as each item
|
||||||
// is added.
|
// is added.
|
||||||
|
@ -613,7 +742,7 @@
|
||||||
this._removeReference(this.models[i]);
|
this._removeReference(this.models[i]);
|
||||||
}
|
}
|
||||||
this._reset();
|
this._reset();
|
||||||
this.add(models, {silent: true, parse: options.parse});
|
this.add(models, _.extend({silent: true}, options));
|
||||||
if (!options.silent) this.trigger('reset', this, options);
|
if (!options.silent) this.trigger('reset', this, options);
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
@ -679,7 +808,8 @@
|
||||||
|
|
||||||
// Prepare a model or hash of attributes to be added to this collection.
|
// Prepare a model or hash of attributes to be added to this collection.
|
||||||
_prepareModel: function(model, options) {
|
_prepareModel: function(model, options) {
|
||||||
if (!(model instanceof Backbone.Model)) {
|
options || (options = {});
|
||||||
|
if (!(model instanceof Model)) {
|
||||||
var attrs = model;
|
var attrs = model;
|
||||||
options.collection = this;
|
options.collection = this;
|
||||||
model = new this.model(attrs, options);
|
model = new this.model(attrs, options);
|
||||||
|
@ -702,12 +832,12 @@
|
||||||
// Sets need to update their indexes when models change ids. All other
|
// Sets need to update their indexes when models change ids. All other
|
||||||
// events simply proxy through. "add" and "remove" events that originate
|
// events simply proxy through. "add" and "remove" events that originate
|
||||||
// in other collections are ignored.
|
// in other collections are ignored.
|
||||||
_onModelEvent: function(ev, model, collection, options) {
|
_onModelEvent: function(event, model, collection, options) {
|
||||||
if ((ev == 'add' || ev == 'remove') && collection != this) return;
|
if ((event == 'add' || event == 'remove') && collection != this) return;
|
||||||
if (ev == 'destroy') {
|
if (event == 'destroy') {
|
||||||
this.remove(model, options);
|
this.remove(model, options);
|
||||||
}
|
}
|
||||||
if (model && ev === 'change:' + model.idAttribute) {
|
if (model && event === 'change:' + model.idAttribute) {
|
||||||
delete this._byId[model.previous(model.idAttribute)];
|
delete this._byId[model.previous(model.idAttribute)];
|
||||||
this._byId[model.id] = model;
|
this._byId[model.id] = model;
|
||||||
}
|
}
|
||||||
|
@ -725,7 +855,7 @@
|
||||||
|
|
||||||
// Mix in each Underscore method as a proxy to `Collection#models`.
|
// Mix in each Underscore method as a proxy to `Collection#models`.
|
||||||
_.each(methods, function(method) {
|
_.each(methods, function(method) {
|
||||||
Backbone.Collection.prototype[method] = function() {
|
Collection.prototype[method] = function() {
|
||||||
return _[method].apply(_, [this.models].concat(_.toArray(arguments)));
|
return _[method].apply(_, [this.models].concat(_.toArray(arguments)));
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
@ -735,7 +865,7 @@
|
||||||
|
|
||||||
// Routers map faux-URLs to actions, and fire events when routes are
|
// Routers map faux-URLs to actions, and fire events when routes are
|
||||||
// matched. Creating a new one sets its `routes` hash, if not set statically.
|
// matched. Creating a new one sets its `routes` hash, if not set statically.
|
||||||
Backbone.Router = function(options) {
|
var Router = Backbone.Router = function(options) {
|
||||||
options || (options = {});
|
options || (options = {});
|
||||||
if (options.routes) this.routes = options.routes;
|
if (options.routes) this.routes = options.routes;
|
||||||
this._bindRoutes();
|
this._bindRoutes();
|
||||||
|
@ -749,7 +879,7 @@
|
||||||
var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
|
var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
|
||||||
|
|
||||||
// Set up all inheritable **Backbone.Router** properties and methods.
|
// Set up all inheritable **Backbone.Router** properties and methods.
|
||||||
_.extend(Backbone.Router.prototype, Backbone.Events, {
|
_.extend(Router.prototype, Events, {
|
||||||
|
|
||||||
// Initialize is an empty function by default. Override it with your own
|
// Initialize is an empty function by default. Override it with your own
|
||||||
// initialization logic.
|
// initialization logic.
|
||||||
|
@ -762,7 +892,7 @@
|
||||||
// });
|
// });
|
||||||
//
|
//
|
||||||
route: function(route, name, callback) {
|
route: function(route, name, callback) {
|
||||||
Backbone.history || (Backbone.history = new Backbone.History);
|
Backbone.history || (Backbone.history = new History);
|
||||||
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
|
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
|
||||||
if (!callback) callback = this[name];
|
if (!callback) callback = this[name];
|
||||||
Backbone.history.route(route, _.bind(function(fragment) {
|
Backbone.history.route(route, _.bind(function(fragment) {
|
||||||
|
@ -815,7 +945,7 @@
|
||||||
|
|
||||||
// Handles cross-browser history management, based on URL fragments. If the
|
// Handles cross-browser history management, based on URL fragments. If the
|
||||||
// browser does not support `onhashchange`, falls back to polling.
|
// browser does not support `onhashchange`, falls back to polling.
|
||||||
Backbone.History = function() {
|
var History = Backbone.History = function() {
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
_.bindAll(this, 'checkUrl');
|
_.bindAll(this, 'checkUrl');
|
||||||
};
|
};
|
||||||
|
@ -827,15 +957,23 @@
|
||||||
var isExplorer = /msie [\w.]+/;
|
var isExplorer = /msie [\w.]+/;
|
||||||
|
|
||||||
// Has the history handling already been started?
|
// Has the history handling already been started?
|
||||||
var historyStarted = false;
|
History.started = false;
|
||||||
|
|
||||||
// Set up all inheritable **Backbone.History** properties and methods.
|
// Set up all inheritable **Backbone.History** properties and methods.
|
||||||
_.extend(Backbone.History.prototype, Backbone.Events, {
|
_.extend(History.prototype, Events, {
|
||||||
|
|
||||||
// The default interval to poll for hash changes, if necessary, is
|
// The default interval to poll for hash changes, if necessary, is
|
||||||
// twenty times a second.
|
// twenty times a second.
|
||||||
interval: 50,
|
interval: 50,
|
||||||
|
|
||||||
|
// Gets the true hash value. Cannot use location.hash directly due to bug
|
||||||
|
// in Firefox where location.hash will always be decoded.
|
||||||
|
getHash: function(windowOverride) {
|
||||||
|
var loc = windowOverride ? windowOverride.location : window.location;
|
||||||
|
var match = loc.href.match(/#(.*)$/);
|
||||||
|
return match ? match[1] : '';
|
||||||
|
},
|
||||||
|
|
||||||
// Get the cross-browser normalized URL fragment, either from the URL,
|
// Get the cross-browser normalized URL fragment, either from the URL,
|
||||||
// the hash, or the override.
|
// the hash, or the override.
|
||||||
getFragment: function(fragment, forcePushState) {
|
getFragment: function(fragment, forcePushState) {
|
||||||
|
@ -845,10 +983,9 @@
|
||||||
var search = window.location.search;
|
var search = window.location.search;
|
||||||
if (search) fragment += search;
|
if (search) fragment += search;
|
||||||
} else {
|
} else {
|
||||||
fragment = window.location.hash;
|
fragment = this.getHash();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fragment = decodeURIComponent(fragment);
|
|
||||||
if (!fragment.indexOf(this.options.root)) fragment = fragment.substr(this.options.root.length);
|
if (!fragment.indexOf(this.options.root)) fragment = fragment.substr(this.options.root.length);
|
||||||
return fragment.replace(routeStripper, '');
|
return fragment.replace(routeStripper, '');
|
||||||
},
|
},
|
||||||
|
@ -856,10 +993,11 @@
|
||||||
// Start the hash change handling, returning `true` if the current URL matches
|
// Start the hash change handling, returning `true` if the current URL matches
|
||||||
// an existing route, and `false` otherwise.
|
// an existing route, and `false` otherwise.
|
||||||
start: function(options) {
|
start: function(options) {
|
||||||
|
if (History.started) throw new Error("Backbone.history has already been started");
|
||||||
|
History.started = true;
|
||||||
|
|
||||||
// Figure out the initial configuration. Do we need an iframe?
|
// Figure out the initial configuration. Do we need an iframe?
|
||||||
// Is pushState desired ... is it available?
|
// Is pushState desired ... is it available?
|
||||||
if (historyStarted) throw new Error("Backbone.history has already been started");
|
|
||||||
this.options = _.extend({}, {root: '/'}, this.options, options);
|
this.options = _.extend({}, {root: '/'}, this.options, options);
|
||||||
this._wantsHashChange = this.options.hashChange !== false;
|
this._wantsHashChange = this.options.hashChange !== false;
|
||||||
this._wantsPushState = !!this.options.pushState;
|
this._wantsPushState = !!this.options.pushState;
|
||||||
|
@ -867,6 +1005,7 @@
|
||||||
var fragment = this.getFragment();
|
var fragment = this.getFragment();
|
||||||
var docMode = document.documentMode;
|
var docMode = document.documentMode;
|
||||||
var oldIE = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7));
|
var oldIE = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7));
|
||||||
|
|
||||||
if (oldIE) {
|
if (oldIE) {
|
||||||
this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
|
this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
|
||||||
this.navigate(fragment);
|
this.navigate(fragment);
|
||||||
|
@ -885,7 +1024,6 @@
|
||||||
// Determine if we need to change the base url, for a pushState link
|
// Determine if we need to change the base url, for a pushState link
|
||||||
// opened by a non-pushState browser.
|
// opened by a non-pushState browser.
|
||||||
this.fragment = fragment;
|
this.fragment = fragment;
|
||||||
historyStarted = true;
|
|
||||||
var loc = window.location;
|
var loc = window.location;
|
||||||
var atRoot = loc.pathname == this.options.root;
|
var atRoot = loc.pathname == this.options.root;
|
||||||
|
|
||||||
|
@ -900,7 +1038,7 @@
|
||||||
// Or if we've started out with a hash-based route, but we're currently
|
// Or if we've started out with a hash-based route, but we're currently
|
||||||
// in a browser where it could be `pushState`-based instead...
|
// in a browser where it could be `pushState`-based instead...
|
||||||
} else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
|
} else if (this._wantsPushState && this._hasPushState && atRoot && loc.hash) {
|
||||||
this.fragment = loc.hash.replace(routeStripper, '');
|
this.fragment = this.getHash().replace(routeStripper, '');
|
||||||
window.history.replaceState({}, document.title, loc.protocol + '//' + loc.host + this.options.root + this.fragment);
|
window.history.replaceState({}, document.title, loc.protocol + '//' + loc.host + this.options.root + this.fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -914,7 +1052,7 @@
|
||||||
stop: function() {
|
stop: function() {
|
||||||
$(window).unbind('popstate', this.checkUrl).unbind('hashchange', this.checkUrl);
|
$(window).unbind('popstate', this.checkUrl).unbind('hashchange', this.checkUrl);
|
||||||
clearInterval(this._checkUrlInterval);
|
clearInterval(this._checkUrlInterval);
|
||||||
historyStarted = false;
|
History.started = false;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Add a route to be tested when the fragment changes. Routes added later
|
// Add a route to be tested when the fragment changes. Routes added later
|
||||||
|
@ -927,10 +1065,10 @@
|
||||||
// calls `loadUrl`, normalizing across the hidden iframe.
|
// calls `loadUrl`, normalizing across the hidden iframe.
|
||||||
checkUrl: function(e) {
|
checkUrl: function(e) {
|
||||||
var current = this.getFragment();
|
var current = this.getFragment();
|
||||||
if (current == this.fragment && this.iframe) current = this.getFragment(this.iframe.location.hash);
|
if (current == this.fragment && this.iframe) current = this.getFragment(this.getHash(this.iframe));
|
||||||
if (current == this.fragment || current == decodeURIComponent(this.fragment)) return false;
|
if (current == this.fragment) return false;
|
||||||
if (this.iframe) this.navigate(current);
|
if (this.iframe) this.navigate(current);
|
||||||
this.loadUrl() || this.loadUrl(window.location.hash);
|
this.loadUrl() || this.loadUrl(this.getHash());
|
||||||
},
|
},
|
||||||
|
|
||||||
// Attempt to load the current URL fragment. If a route succeeds with a
|
// Attempt to load the current URL fragment. If a route succeeds with a
|
||||||
|
@ -953,12 +1091,12 @@
|
||||||
//
|
//
|
||||||
// The options object can contain `trigger: true` if you wish to have the
|
// The options object can contain `trigger: true` if you wish to have the
|
||||||
// route callback be fired (not usually desirable), or `replace: true`, if
|
// route callback be fired (not usually desirable), or `replace: true`, if
|
||||||
// you which to modify the current URL without adding an entry to the history.
|
// you wish to modify the current URL without adding an entry to the history.
|
||||||
navigate: function(fragment, options) {
|
navigate: function(fragment, options) {
|
||||||
if (!historyStarted) return false;
|
if (!History.started) return false;
|
||||||
if (!options || options === true) options = {trigger: options};
|
if (!options || options === true) options = {trigger: options};
|
||||||
var frag = (fragment || '').replace(routeStripper, '');
|
var frag = (fragment || '').replace(routeStripper, '');
|
||||||
if (this.fragment == frag || this.fragment == decodeURIComponent(frag)) return;
|
if (this.fragment == frag) return;
|
||||||
|
|
||||||
// If pushState is available, we use it to set the fragment as a real URL.
|
// If pushState is available, we use it to set the fragment as a real URL.
|
||||||
if (this._hasPushState) {
|
if (this._hasPushState) {
|
||||||
|
@ -971,7 +1109,7 @@
|
||||||
} else if (this._wantsHashChange) {
|
} else if (this._wantsHashChange) {
|
||||||
this.fragment = frag;
|
this.fragment = frag;
|
||||||
this._updateHash(window.location, frag, options.replace);
|
this._updateHash(window.location, frag, options.replace);
|
||||||
if (this.iframe && (frag != this.getFragment(this.iframe.location.hash))) {
|
if (this.iframe && (frag != this.getFragment(this.getHash(this.iframe)))) {
|
||||||
// Opening and closing the iframe tricks IE7 and earlier to push a history entry on hash-tag change.
|
// Opening and closing the iframe tricks IE7 and earlier to push a history entry on hash-tag change.
|
||||||
// When replace is true, we don't want this.
|
// When replace is true, we don't want this.
|
||||||
if(!options.replace) this.iframe.document.open().close();
|
if(!options.replace) this.iframe.document.open().close();
|
||||||
|
@ -1002,7 +1140,7 @@
|
||||||
|
|
||||||
// Creating a Backbone.View creates its initial element outside of the DOM,
|
// Creating a Backbone.View creates its initial element outside of the DOM,
|
||||||
// if an existing element is not provided...
|
// if an existing element is not provided...
|
||||||
Backbone.View = function(options) {
|
var View = Backbone.View = function(options) {
|
||||||
this.cid = _.uniqueId('view');
|
this.cid = _.uniqueId('view');
|
||||||
this._configure(options || {});
|
this._configure(options || {});
|
||||||
this._ensureElement();
|
this._ensureElement();
|
||||||
|
@ -1011,13 +1149,13 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cached regex to split keys for `delegate`.
|
// Cached regex to split keys for `delegate`.
|
||||||
var eventSplitter = /^(\S+)\s*(.*)$/;
|
var delegateEventSplitter = /^(\S+)\s*(.*)$/;
|
||||||
|
|
||||||
// List of view options to be merged as properties.
|
// List of view options to be merged as properties.
|
||||||
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];
|
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];
|
||||||
|
|
||||||
// Set up all inheritable **Backbone.View** properties and methods.
|
// Set up all inheritable **Backbone.View** properties and methods.
|
||||||
_.extend(Backbone.View.prototype, Backbone.Events, {
|
_.extend(View.prototype, Events, {
|
||||||
|
|
||||||
// The default `tagName` of a View's element is `"div"`.
|
// The default `tagName` of a View's element is `"div"`.
|
||||||
tagName: 'div',
|
tagName: 'div',
|
||||||
|
@ -1061,7 +1199,8 @@
|
||||||
// Change the view's element (`this.el` property), including event
|
// Change the view's element (`this.el` property), including event
|
||||||
// re-delegation.
|
// re-delegation.
|
||||||
setElement: function(element, delegate) {
|
setElement: function(element, delegate) {
|
||||||
this.$el = $(element);
|
if (this.$el) this.undelegateEvents();
|
||||||
|
this.$el = (element instanceof $) ? element : $(element);
|
||||||
this.el = this.$el[0];
|
this.el = this.$el[0];
|
||||||
if (delegate !== false) this.delegateEvents();
|
if (delegate !== false) this.delegateEvents();
|
||||||
return this;
|
return this;
|
||||||
|
@ -1088,8 +1227,8 @@
|
||||||
for (var key in events) {
|
for (var key in events) {
|
||||||
var method = events[key];
|
var method = events[key];
|
||||||
if (!_.isFunction(method)) method = this[events[key]];
|
if (!_.isFunction(method)) method = this[events[key]];
|
||||||
if (!method) throw new Error('Event "' + events[key] + '" does not exist');
|
if (!method) throw new Error('Method "' + events[key] + '" does not exist');
|
||||||
var match = key.match(eventSplitter);
|
var match = key.match(delegateEventSplitter);
|
||||||
var eventName = match[1], selector = match[2];
|
var eventName = match[1], selector = match[2];
|
||||||
method = _.bind(method, this);
|
method = _.bind(method, this);
|
||||||
eventName += '.delegateEvents' + this.cid;
|
eventName += '.delegateEvents' + this.cid;
|
||||||
|
@ -1145,8 +1284,7 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set up inheritance for the model, collection, and view.
|
// Set up inheritance for the model, collection, and view.
|
||||||
Backbone.Model.extend = Backbone.Collection.extend =
|
Model.extend = Collection.extend = Router.extend = View.extend = extend;
|
||||||
Backbone.Router.extend = Backbone.View.extend = extend;
|
|
||||||
|
|
||||||
// Backbone.sync
|
// Backbone.sync
|
||||||
// -------------
|
// -------------
|
||||||
|
@ -1177,6 +1315,9 @@
|
||||||
Backbone.sync = function(method, model, options) {
|
Backbone.sync = function(method, model, options) {
|
||||||
var type = methodMap[method];
|
var type = methodMap[method];
|
||||||
|
|
||||||
|
// Default options, unless specified.
|
||||||
|
options || (options = {});
|
||||||
|
|
||||||
// Default JSON-request options.
|
// Default JSON-request options.
|
||||||
var params = {type: type, dataType: 'json'};
|
var params = {type: type, dataType: 'json'};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue