Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Model.get fallback callback/value. #1184

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions backbone.js
Expand Up @@ -237,8 +237,10 @@
},

// Get the value of an attribute.
get: function(attr) {
return this.attributes[attr];
get: function(attr, fallback) {
if (this.has(attr)) return this.attributes[attr];
if (_.isFunction(fallback)) fallback = fallback.call(this);
return fallback;
},

// Get the HTML-escaped value of an attribute.
Expand All @@ -252,7 +254,7 @@
// Returns `true` if the attribute contains a value that is not null
// or undefined.
has: function(attr) {
return this.get(attr) != null;
return this.attributes[attr] != null;
},

// Set a hash of model attributes on the object, firing `"change"` unless
Expand Down Expand Up @@ -1428,4 +1430,4 @@
throw new Error('A "url" property or function must be specified');
};

}).call(this);
}).call(this);