From 59f6a1c1d747f6b90751e93bcc6662621f24d3e9 Mon Sep 17 00:00:00 2001 From: Ben Pickles Date: Fri, 8 Oct 2010 21:53:13 +0100 Subject: [PATCH] Editing an object after it has been used as the starting attributes of a model shouldn't affect the model's attributes. --- src/model.js | 2 +- test/tests/model.js | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/model.js b/src/model.js index c0db8d6..48739a9 100644 --- a/src/model.js +++ b/src/model.js @@ -4,7 +4,7 @@ var Model = function(name, class_methods, instance_methods) { // The model constructor. var model = function(attributes) { - this.attributes = attributes || {}; + this.attributes = jQuery.extend({}, attributes) this.changes = {}; this.errors = new Model.Errors(this); this.uid = [name, Model.UID.generate()].join("-") diff --git a/test/tests/model.js b/test/tests/model.js index fc5cda4..0133386 100644 --- a/test/tests/model.js +++ b/test/tests/model.js @@ -1,10 +1,20 @@ module("Model"); -test("attr, attributes, changes, reset, save, destroy", function() { - var Post = Model("post"); +test("defining attributes when instanciating a model", function() { + var Post = Model("post") + var post + + post = new Post(undefined) + same({}, post.attributes) - same(new Post().attributes, {}); + var attributes = { a: "a", b: "b" } + post = new Post(attributes) + attributes.a = "b" + same("a", post.attributes.a) +}) +test("attr, attributes, changes, reset, save, destroy", function() { + var Post = Model("post"); var post = new Post({ title: "Foo", body: "..." }); same(post.attributes, { title: "Foo", body: "..." });