From 42d321f236f15ffeabe525a0358cc401467678ab Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 17 Jan 2012 11:55:15 -0500 Subject: [PATCH] Fixes #570, supports Model#save(key, value) --- backbone.js | 11 ++++++++++- test/model.js | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/backbone.js b/backbone.js index bb4ebf3b..fa0dba72 100644 --- a/backbone.js +++ b/backbone.js @@ -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; diff --git a/test/model.js b/test/model.js index e60dba69..19806682 100644 --- a/test/model.js +++ b/test/model.js @@ -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');