diff --git a/backbone.js b/backbone.js index 8438e727..1e8f506c 100644 --- a/backbone.js +++ b/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.