Skip to content

Commit

Permalink
parse:true runs the attributes through parse():
Browse files Browse the repository at this point in the history
`new Model(attr, {parse:true})` will now call
Model.prototype.parse(attr). This is useful if
you want to create a model out of an object
structure from an external server.
  • Loading branch information
judofyr committed Dec 4, 2011
1 parent 8adac91 commit bdbcfa9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
Backbone.Model = function(attributes, options) {
var defaults;
attributes || (attributes = {});
if (options && options.parse) attributes = this.parse(attributes);
if (defaults = this.defaults) {
if (_.isFunction(defaults)) defaults = defaults.call(this);
attributes = _.extend({}, defaults, attributes);
Expand Down
11 changes: 11 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ $(document).ready(function() {
equals(model.one, 1);
});

test("Model: initialize with parsed attributes", function() {
var Model = Backbone.Model.extend({
parse: function(obj) {
obj.value += 1;
return obj;
}
});
var model = new Model({value: 1}, {parse: true});
equals(model.get('value'), 2);
});

test("Model: url", function() {
equals(doc.url(), '/collection/1-the-tempest');
doc.collection.url = '/collection/';
Expand Down

0 comments on commit bdbcfa9

Please sign in to comment.