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

Updated isDirty function to check individual attributes #28

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
40 changes: 24 additions & 16 deletions model/backup/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,41 @@ See this in action:
* @parent jquery.model.backup
* Returns if the instance needs to be saved. This will go
* through associations too.
* @param {Boolean} [checkAssociations=false] true if associations should be checked. Defaults to false.
* be checked, false if otherwise
* @param {Object} [option=false] if a string is passed in then the attribute with that name is checked for dirtiness. Otherwise
* this parameter determines if associations should be checked. Defaults to false.
* @return {Boolean} true if there are changes, false if otherwise
*/
isDirty: function(checkAssociations) {
isDirty: function(option) {
if(!this._backupStore) return false;
//go through attrs and compare ...
var current = this.attrs(),
name,
association,
res;
for(name in current){
if(current[name] !== this._backupStore[name]){
return true;
}

if(typeof option === "string") {
var current = this.attrs();

return current[option] !== this._backupStore[option] ? true : false;
}
if( checkAssociations ){
res = associations(this, function(associated){
return associated.isDirty();
})
if(res === true){
return true;
else {
for(name in current){
if(current[name] !== this._backupStore[name]){
return true;
}
}
if( option ){
res = associations(this, function(associated){
return associated.isDirty();
})
if(res === true){
return true;
}
}

return false;
}

return false;
},
/**
* @function jQuery.Model.prototype.restore
Expand Down
15 changes: 15 additions & 0 deletions model/backup/qunit/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ test("backup / restore with associations", function(){

equals(recipe.name, "cheese burger" ,"name back");

ok(!recipe.isDirty('name'), "name attr not dirty");

recipe.name = 'dirty name';

ok(recipe.isDirty('name'), "name attr is dirty");

// test non-existence attribute
ok(!recipe.isDirty('bogusAttr'), "not dirty since it does not exist");

recipe.restore();

ok(!recipe.isDirty(), "restored, clean");

equals(recipe.name, "cheese burger" ,"name back");

// test belongs too

ok(!recipe.cookbook.isDirty(), "cookbook not backedup, but clean");
Expand Down