diff --git a/model/backup/backup.js b/model/backup/backup.js index 65290f3e..b096328a 100644 --- a/model/backup/backup.js +++ b/model/backup/backup.js @@ -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 diff --git a/model/backup/qunit/qunit.js b/model/backup/qunit/qunit.js index a30ca1a4..953f8e2f 100644 --- a/model/backup/qunit/qunit.js +++ b/model/backup/qunit/qunit.js @@ -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");