From e664e055671a364045016787c6dd449773ba731e Mon Sep 17 00:00:00 2001 From: Nathan Palmer Date: Thu, 19 Jul 2012 10:45:09 -0400 Subject: [PATCH] Added Attribute to unbind correctly --- spec/SpineDataBindSpec.js | 28 ++++++++++++++++++++++++++++ src/spine.databind.coffee | 2 +- src/spine.databind.js | 5 +---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/spec/SpineDataBindSpec.js b/spec/SpineDataBindSpec.js index 4cc9445..49383c6 100644 --- a/spec/SpineDataBindSpec.js +++ b/spec/SpineDataBindSpec.js @@ -895,6 +895,34 @@ describe("Spine.DataBind", function() { var a = $('#homepage'); expect(a.attr('href')).toBe('http://www.example.com'); }); + + it("should unbind without destroying other bindings", function() { + if (Watch) { + Person.bind("update[homepage]", function() { console.log("update"); }); + expect(Person.constructor._callbacks["update[homepage]"].length).toBe(2); + Person.trigger("destroy-bindings"); + expect(Person.constructor._callbacks["update[homepage]"].length).toBe(1); + } else { + Person.bind("update", function() { console.log("update"); }); + expect(Person.constructor._callbacks["update"].length).toBe(2); + Person.trigger("destroy-bindings"); + expect(Person.constructor._callbacks["update"].length).toBe(1); + } + }); + + it("should rebind without destroying other bindings", function() { + if (Watch) { + Person.bind("update[homepage]", function() { console.log("update"); }); + expect(Person.constructor._callbacks["update[homepage]"].length).toBe(2); + Controller.refreshBindings(Person); + expect(Person.constructor._callbacks["update[homepage]"].length).toBe(2); + } else { + Person.bind("update", function() { console.log("update"); }); + expect(Person.constructor._callbacks["update"].length).toBe(2); + Controller.refreshBindings(Person); + expect(Person.constructor._callbacks["update"].length).toBe(2); + } + }); }; describe("with bindings", function() { diff --git a/src/spine.databind.coffee b/src/spine.databind.coffee index f8866f9..cc8d12d 100644 --- a/src/spine.databind.coffee +++ b/src/spine.databind.coffee @@ -224,7 +224,7 @@ class Attribute extends Template keys: [ "attr" ] bind: (operators,model,el,options) -> - model.bind("update", => @update(operators,model,el,options)) + @init(operators,model,el,options,"update") @update(operators,model,el,options) unbind: (operators,model,el,options) -> diff --git a/src/spine.databind.js b/src/spine.databind.js index 6927c74..cfa4585 100644 --- a/src/spine.databind.js +++ b/src/spine.databind.js @@ -432,10 +432,7 @@ Attribute.prototype.keys = ["attr"]; Attribute.prototype.bind = function(operators, model, el, options) { - var _this = this; - model.bind("update", function() { - return _this.update(operators, model, el, options); - }); + this.init(operators, model, el, options, "update"); return this.update(operators, model, el, options); };