mirror of
https://github.com/jashkenas/backbone.git
synced 2026-01-23 13:58:06 -05:00
unifying property-or-function logic.
This commit is contained in:
18
backbone.js
18
backbone.js
@@ -128,8 +128,7 @@
|
||||
var defaults;
|
||||
attributes || (attributes = {});
|
||||
if (options && options.parse) attributes = this.parse(attributes);
|
||||
if (defaults = this.defaults) {
|
||||
if (_.isFunction(defaults)) defaults = defaults.call(this);
|
||||
if (defaults = getValue(this, 'defaults')) {
|
||||
attributes = _.extend({}, defaults, attributes);
|
||||
}
|
||||
this.attributes = {};
|
||||
@@ -293,7 +292,7 @@
|
||||
// using Backbone's restful methods, override this to change the endpoint
|
||||
// that will be called.
|
||||
url : function() {
|
||||
var base = getUrl(this.collection) || this.urlRoot || urlError();
|
||||
var base = getValue(this.collection, 'url') || this.urlRoot || urlError();
|
||||
if (this.isNew()) return base;
|
||||
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
|
||||
},
|
||||
@@ -938,8 +937,7 @@
|
||||
// This only works for delegate-able events: not `focus`, `blur`, and
|
||||
// not `change`, `submit`, and `reset` in Internet Explorer.
|
||||
delegateEvents : function(events) {
|
||||
if (!(events || (events = this.events))) return;
|
||||
if (_.isFunction(events)) events = events.call(this);
|
||||
if (!(events || (events = getValue(this, 'events')))) return;
|
||||
this.undelegateEvents();
|
||||
for (var key in events) {
|
||||
var method = this[events[key]];
|
||||
@@ -1035,7 +1033,7 @@
|
||||
|
||||
// Ensure that we have a URL.
|
||||
if (!options.url) {
|
||||
params.url = getUrl(model) || urlError();
|
||||
params.url = getValue(model, 'url') || urlError();
|
||||
}
|
||||
|
||||
// Ensure that we have the appropriate request data.
|
||||
@@ -1116,11 +1114,11 @@
|
||||
return child;
|
||||
};
|
||||
|
||||
// Helper function to get a URL from a Model or Collection as a property
|
||||
// Helper function to get a value from a Backbone object as a property
|
||||
// or as a function.
|
||||
var getUrl = function(object) {
|
||||
if (!(object && object.url)) return null;
|
||||
return _.isFunction(object.url) ? object.url() : object.url;
|
||||
var getValue = function(object, prop) {
|
||||
if (!(object && object[prop])) return null;
|
||||
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
|
||||
};
|
||||
|
||||
// Throw an error when a URL is needed, and none is supplied.
|
||||
|
||||
Reference in New Issue
Block a user