diff --git a/spec/SpineDataBindSpec.js b/spec/SpineDataBindSpec.js index 446c1d9..4cc9445 100644 --- a/spec/SpineDataBindSpec.js +++ b/spec/SpineDataBindSpec.js @@ -747,6 +747,34 @@ describe("Spine.DataBind", function() { var reset = $('#reset'); expect(reset.css('display')).toNotBe('none'); }); + + it("should unbind without destroying other bindings", function() { + if (Watch) { + Person.bind("update[phoneNumberCount]", function() { console.log("update"); }); + expect(Person.constructor._callbacks["update[phoneNumberCount]"].length).toBe(2); + Person.trigger("destroy-bindings"); + expect(Person.constructor._callbacks["update[phoneNumberCount]"].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[phoneNumberCount]", function() { console.log("update"); }); + expect(Person.constructor._callbacks["update[phoneNumberCount]"].length).toBe(2); + Controller.refreshBindings(Person); + expect(Person.constructor._callbacks["update[phoneNumberCount]"].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 773d3e1..f8866f9 100644 --- a/src/spine.databind.coffee +++ b/src/spine.databind.coffee @@ -199,9 +199,10 @@ class Visible extends Template bind: (operators,model,el,options) -> if options.watch - model.bind("update["+operator.property+"]", => @update([operator],model,el,options)) for operator in operators + @init([operator],model,el,options,"update["+operator.property+"]") for operator in operators else - 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 94ecd0f..6927c74 100644 --- a/src/spine.databind.js +++ b/src/spine.databind.js @@ -378,19 +378,14 @@ Visible.prototype.keys = ["visible"]; Visible.prototype.bind = function(operators, model, el, options) { - var operator, _i, _len, - _this = this; + var operator, _i, _len; if (options.watch) { for (_i = 0, _len = operators.length; _i < _len; _i++) { operator = operators[_i]; - model.bind("update[" + operator.property + "]", function() { - return _this.update([operator], model, el, options); - }); + this.init([operator], model, el, options, "update[" + operator.property + "]"); } } else { - 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); };