Fixes #570, supports Model#save(key, value)

This commit is contained in:
Jeremy Ashkenas
2012-01-17 11:55:15 -05:00
parent 09110a6656
commit 42d321f236
2 changed files with 19 additions and 1 deletions

View File

@@ -291,7 +291,16 @@
// Set a hash of model attributes, and sync the model to the server.
// If the server returns an attributes hash that differs, the model's
// state will be `set` again.
save : function(attrs, options) {
save : function(key, value, options) {
var attrs;
if (_.isObject(key) || key == null) {
attrs = key;
options = value;
} else {
attrs = {};
attrs[key] = value;
}
options = options ? _.clone(options) : {};
if (attrs && !this.set(attrs, options)) return false;
var model = this;

View File

@@ -331,6 +331,15 @@ $(document).ready(function() {
ok(_.isEqual(lastRequest[1], doc));
});
test("Model: save in positional style", function() {
var model = new Backbone.Model();
model.sync = function(method, model, options) {
options.success();
};
model.save('title', 'Twelfth Night');
equals(model.get('title'), 'Twelfth Night');
});
test("Model: fetch", function() {
doc.fetch();
ok(lastRequest[0], 'read');