From 03ac5a84c282ee597c36fc05ddd4ab8f7f4add1e Mon Sep 17 00:00:00 2001 From: Michael Malamud Date: Thu, 28 Apr 2011 12:40:04 -0400 Subject: [PATCH 1/5] Added isAttrDirty function --- model/backup/backup.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/model/backup/backup.js b/model/backup/backup.js index 65290f3e..7ff3d426 100644 --- a/model/backup/backup.js +++ b/model/backup/backup.js @@ -117,6 +117,12 @@ See this in action: return false; }, + isAttrDirty: function(attr) { + if(!this._backupStore) return false; + var current = this.attrs(); + + return current[attr] !== this._backupStore[attr] ? true : false; + }, /** * @function jQuery.Model.prototype.restore * @plugin jquery/model/backup From e31a8b3c992b8cad123c80ae00de16b2abae7dcf Mon Sep 17 00:00:00 2001 From: Michael Malamud Date: Thu, 28 Apr 2011 12:40:39 -0400 Subject: [PATCH 2/5] Added unit tests for isAttrDirty --- model/backup/qunit/qunit.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/model/backup/qunit/qunit.js b/model/backup/qunit/qunit.js index a30ca1a4..8408ae7f 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.isAttrDirty('name'), "name attr not dirty"); + + recipe.name = 'dirty name'; + + ok(recipe.isAttrDirty('name'), "name attr is dirty"); + + // test non-existence attribute + ok(!recipe.isAttrDirty('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"); From 121ddaa64f90ea55c2737af9bbfed17d06c62458 Mon Sep 17 00:00:00 2001 From: Michael Malamud Date: Thu, 28 Apr 2011 14:58:31 -0400 Subject: [PATCH 3/5] Moved isAttrDirty into isDirty --- model/backup/backup.js | 47 ++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/model/backup/backup.js b/model/backup/backup.js index 7ff3d426..53d85508 100644 --- a/model/backup/backup.js +++ b/model/backup/backup.js @@ -89,39 +89,42 @@ 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") { + if(!this._backupStore) return false; + 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; - }, - isAttrDirty: function(attr) { - if(!this._backupStore) return false; - var current = this.attrs(); - - return current[attr] !== this._backupStore[attr] ? true : false; }, /** * @function jQuery.Model.prototype.restore From caba7e3794549a14fa595a5a55c1935c9c8e6432 Mon Sep 17 00:00:00 2001 From: Michael Malamud Date: Thu, 28 Apr 2011 14:59:37 -0400 Subject: [PATCH 4/5] Updated tests for isDirty that allows for passing in attribute name --- model/backup/qunit/qunit.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/model/backup/qunit/qunit.js b/model/backup/qunit/qunit.js index 8408ae7f..953f8e2f 100644 --- a/model/backup/qunit/qunit.js +++ b/model/backup/qunit/qunit.js @@ -66,14 +66,14 @@ test("backup / restore with associations", function(){ equals(recipe.name, "cheese burger" ,"name back"); - ok(!recipe.isAttrDirty('name'), "name attr not dirty"); + ok(!recipe.isDirty('name'), "name attr not dirty"); recipe.name = 'dirty name'; - ok(recipe.isAttrDirty('name'), "name attr is dirty"); + ok(recipe.isDirty('name'), "name attr is dirty"); // test non-existence attribute - ok(!recipe.isAttrDirty('bogusAttr'), "not dirty since it does not exist"); + ok(!recipe.isDirty('bogusAttr'), "not dirty since it does not exist"); recipe.restore(); From 1deed678bec413ebef89f7ac0cdef6d245d53750 Mon Sep 17 00:00:00 2001 From: Michael Malamud Date: Thu, 28 Apr 2011 15:07:40 -0400 Subject: [PATCH 5/5] We are already doing this check in the first line of the function --- model/backup/backup.js | 1 - 1 file changed, 1 deletion(-) diff --git a/model/backup/backup.js b/model/backup/backup.js index 53d85508..b096328a 100644 --- a/model/backup/backup.js +++ b/model/backup/backup.js @@ -102,7 +102,6 @@ See this in action: res; if(typeof option === "string") { - if(!this._backupStore) return false; var current = this.attrs(); return current[option] !== this._backupStore[option] ? true : false;