Skip to content
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

Add some tests for change() #59

Open
wants to merge 5 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
36 changes: 14 additions & 22 deletions backbone-nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,11 @@
Backbone.NestedModel = Backbone.Model.extend({

get: function(attrStrOrPath){
var attrPath = Backbone.NestedModel.attrPath(attrStrOrPath),
result;
var attrPath = Backbone.NestedModel.attrPath(attrStrOrPath);

Backbone.NestedModel.walkPath(this.attributes, attrPath, function(val, path){
var attr = _.last(path);
if (path.length === attrPath.length){
// attribute found
result = val[attr];
}
});

return result;
},

has: function(attr){
// for some reason this is not how Backbone.Model is implemented - it accesses the attributes object directly
var result = this.get(attr);
return !(result === null || _.isUndefined(result));
return _.reduce(attrPath, function(val, attr) {
return _.isObject(val) ? val[attr] : undefined;
}, this.attributes);
},

set: function(key, value, opts){
Expand Down Expand Up @@ -115,7 +102,7 @@
if (_.isObject(val)) { // clear child attrs
setChanged(val, changedPath);
}
if (!options.silent) model._delayedChange(changedPath, null);
model._delayedChange(changedPath, null);
changed[changedPath] = null;
});
};
Expand All @@ -124,11 +111,16 @@
this.attributes = {};
this._escapedAttributes = {};

this._delayedTrigger('change');

// Fire the `"change"` events.
if (!options.silent) this._delayedTrigger('change');
if (!options.silent) this._runDelayedTriggers();
return this;
},

change: function() {
this._runDelayedTriggers();
return this;
return Backbone.NestedModel.__super__.change.apply(this, _.toArray(arguments));
},

add: function(attrStr, value, opts){
Expand Down Expand Up @@ -175,7 +167,6 @@
return Backbone.NestedModel.deepClone(this.attributes);
},


// private
_delayedTrigger: function(/* the trigger args */){
_delayedTriggers.push(arguments);
Expand Down Expand Up @@ -253,7 +244,7 @@
}
}

if (!opts.silent){
if (!opts.silent) {
// let the superclass handle change events for top-level attributes
if (path.length > 1 && isNewValue){
model._delayedChange(attrStr, val[attr]);
Expand All @@ -263,6 +254,7 @@
model._delayedTrigger('add:' + attrStr, model, val[attr]);
}
}

});
}

Expand Down
62 changes: 48 additions & 14 deletions test/nested-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $(document).ready(function() {

test("#get() 1-1 returns attributes object", function() {
var name = doc.get('name');

deepEqual(name, {
first: 'Aidan',
middle: {
Expand Down Expand Up @@ -322,7 +322,7 @@ $(document).ready(function() {
var changeNameFirst = sinon.spy();
var changeNameLast = sinon.spy();
var changeGender = sinon.spy();

doc.bind('change', change);
doc.bind('change:name', changeName);
doc.bind('change:name.first', changeNameFirst);
Expand Down Expand Up @@ -356,6 +356,23 @@ $(document).ready(function() {
sinon.assert.notCalled(changeNameFirst);
});

test("change() fires the silenced events", function() {
var change = sinon.spy();
var changeName = sinon.spy();
var changeNameFirst = sinon.spy();

doc.bind('change', change);
doc.bind('change:name', changeName);
doc.bind('change:name.first', changeNameFirst);

doc.set({'name.first': 'Bob'}, {silent: true});
doc.change();

sinon.assert.calledOnce(change);
sinon.assert.calledOnce(changeName);
sinon.assert.calledOnce(changeNameFirst);
});

test("change event doesn't fire if new value matches old value", function() {
var change = sinon.spy();
var changeName = sinon.spy();
Expand Down Expand Up @@ -504,7 +521,7 @@ $(document).ready(function() {
var changeNameMiddleInitial = sinon.spy();
var changeNameMiddleFull = sinon.spy();
var changeNameFirst = sinon.spy();

doc.bind('change', change);
doc.bind('change:name', changeName);
doc.bind('change:name.middle', changeNameMiddle);
Expand Down Expand Up @@ -536,12 +553,12 @@ $(document).ready(function() {
var changeAddresses0City = sinon.spy();
var changeAddresses0State = sinon.spy();
var changeAddresses1 = sinon.spy();

doc.bind('change', change);
doc.bind('change:addresses', changeAddresses);
doc.bind('change:addresses[0]', changeAddresses0);
doc.bind('change:addresses[0].city', changeAddresses0City);

doc.bind('change:addresses[0].state', changeAddresses0State);
doc.bind('change:addresses[1]', changeAddresses1);

Expand All @@ -563,7 +580,7 @@ $(document).ready(function() {
var change = sinon.spy();
var changeAddresses = sinon.spy();
var addAddresses = sinon.spy();

doc.bind('change', change);
doc.bind('change:addresses', changeAddresses);
doc.bind('add:addresses', addAddresses);
Expand All @@ -585,7 +602,7 @@ $(document).ready(function() {
var change = sinon.spy();
var changeAddresses = sinon.spy();
var removeAddresses = sinon.spy();

doc.bind('change', change);
doc.bind('change:addresses', changeAddresses);
doc.bind('remove:addresses', removeAddresses);
Expand All @@ -601,7 +618,7 @@ $(document).ready(function() {
var change = sinon.spy();
var changeAddresses = sinon.spy();
var removeAddresses = sinon.spy();

doc.bind('change', change);
doc.bind('change:addresses', changeAddresses);
doc.bind('remove:addresses', removeAddresses);
Expand All @@ -625,7 +642,7 @@ $(document).ready(function() {
full: 'Limburger',
fullAlternates: ['Danger', 'Funny', 'Responsible']
});

doc.bind('change', change);
doc.bind('change:name', changeName);
doc.bind('change:name.middle', changeNameMiddle);
Expand Down Expand Up @@ -662,7 +679,7 @@ $(document).ready(function() {
'name.middle.full': 'Limburger'
});
});

doc.set({'name.middle.full': 'Limburger'});
});

Expand All @@ -684,7 +701,7 @@ $(document).ready(function() {
'name.middle.full': 'Limburger'
});
});

//Set using conventional JSON - emulates a model fetch
doc.set({
gender: 'M',
Expand All @@ -708,7 +725,7 @@ $(document).ready(function() {
]
});
});

test("#changedAttributes() should clear the nested attributes between change events", function() {
doc.set({'name.first': 'Bob'});

Expand All @@ -735,7 +752,7 @@ $(document).ready(function() {
return "First name is too long";
}
};

doc.set({'name.first': 'TooLongFirstName'});

doc.bind('change', function(){
Expand Down Expand Up @@ -819,6 +836,23 @@ $(document).ready(function() {
sinon.assert.notCalled(changeNameFirst);
});

test("#clear() silent triggers change events when change() is called", function() {
var change = sinon.spy();
var changeName = sinon.spy();
var changeNameFirst = sinon.spy();

doc.bind('change', change);
doc.bind('change:name', changeName);
doc.bind('change:name.first', changeNameFirst);

doc.clear({silent: true});
doc.change();

sinon.assert.calledOnce(change);
sinon.assert.calledOnce(changeName);
sinon.assert.calledOnce(changeNameFirst);
});


// ----- UNSET --------

Expand Down Expand Up @@ -957,7 +991,7 @@ $(document).ready(function() {
doc.bind('remove:addresses', removeAddresses);

doc.remove('addresses[0]');

sinon.assert.calledOnce(removeAddresses);
equal(doc.get('addresses').length, 1);

Expand Down