From 12a17c630c04729f686fed69ea48e12d4ff81f23 Mon Sep 17 00:00:00 2001 From: fcamblor Date: Thu, 16 Apr 2015 21:28:13 +0200 Subject: [PATCH 1/2] Provided tests for idAttribute --- test/nested-model.js | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/nested-model.js b/test/nested-model.js index 6610b50..a54c471 100644 --- a/test/nested-model.js +++ b/test/nested-model.js @@ -301,6 +301,57 @@ $(document).ready(function() { deepEqual(doc.get('addresses[0].areaCodes'), []); }); + test("#construct with idAttribute with simple path (non regression test)", function(){ + var Clazz = Backbone.NestedModel.extend({ + idAttribute: "foo" + }); + + var d1 = new Clazz({ bar: 'bar' }); + var d2 = new Clazz({ foo: 'foo' }); + + equal(d1.id, null); + equal(d2.id, 'foo'); + }); + + test("#set() with idAttribute with simple path (non regression test)", function(){ + var Clazz = Backbone.NestedModel.extend({ + idAttribute: "foo" + }); + + var d1 = new Clazz(); d1.set({ bar: 'bar' }); + var d2 = new Clazz(); d2.set({ foo: 'foo' }); + + equal(d1.id, null); + equal(d2.id, 'foo'); + }); + + test("#construct with idAttribute and nested path", function(){ + var Clazz = Backbone.NestedModel.extend({ + idAttribute: "foo.bar" + }); + + var d1 = new Clazz({ bar: 'bar' }); + var d2 = new Clazz({ foo: { foo2: 'foo-bar' } }); + var d3 = new Clazz({ foo: { bar: 'foo-bar' } }); + + equal(d1.id, null); + equal(d2.id, null); + equal(d3.id, 'foo-bar'); + }); + + test("#set() with idAttribute and nested path", function(){ + var Clazz = Backbone.NestedModel.extend({ + idAttribute: "foo.bar" + }); + + var d1 = new Clazz(); d1.set({ bar: 'bar' }); + var d2 = new Clazz(); d2.set({ foo: { foo2: 'foo-bar' } }); + var d3 = new Clazz(); d3.set({ foo: { bar: 'foo-bar' } }); + + equal(d1.id, null); + equal(d2.id, null); + equal(d3.id, 'foo-bar'); + }); // ----- TO_JSON -------- From 0b283cdd3f3ea18248f18086cf4a74f7df8d7123 Mon Sep 17 00:00:00 2001 From: fcamblor Date: Thu, 16 Apr 2015 21:28:40 +0200 Subject: [PATCH 2/2] Provided idAttribute support on NestedModels --- backbone-nested.js | 1 + 1 file changed, 1 insertion(+) diff --git a/backbone-nested.js b/backbone-nested.js index 0bd6a46..41c7cf7 100644 --- a/backbone-nested.js +++ b/backbone-nested.js @@ -100,6 +100,7 @@ validated = Backbone.NestedModel.__super__.set.call(this, unsetObj, opts); } + if(this.idAttribute && this.idAttribute.indexOf(".") !== -1) this.id = this.get(this.idAttribute); if (!validated){ // reset changed attributes