Skip to content

Fix idAttribute #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backbone-nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions test/nested-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------

Expand Down