Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Added Enable and refactored how the bindings were getting initted
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanpalmer committed Jul 19, 2012
1 parent 0e00710 commit 00c2be7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 88 deletions.
16 changes: 15 additions & 1 deletion spec/SpineDataBindSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,21 @@ describe("Spine.DataBind", function() {

var reset = $('#reset');
expect(reset.attr('disabled')).toBe(undefined);
});
});

it("should unbind without destroying other bindings", function() {
Person.bind("change", function() { console.log("update"); });
expect(Person.constructor._callbacks["change"].length).toBe(2);
Person.trigger("destroy-bindings");
expect(Person.constructor._callbacks["change"].length).toBe(1);
});

it("should rebind without destroying other bindings", function() {
Person.bind("change", function() { console.log("update"); });
expect(Person.constructor._callbacks["change"].length).toBe(2);
Controller.refreshBindings(Person);
expect(Person.constructor._callbacks["change"].length).toBe(2);
});
};

describe("with bindings", function() {
Expand Down
56 changes: 21 additions & 35 deletions src/spine.databind.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
class Template
keys: [ ]

bind: (operators,model,el,options) ->
unbind: (operators,model,el,options) ->

init: (operators,model,el,options,event) ->
model.constructor.bind(event, binder = () => @update(operators,model,el,options))
model.constructor.bind("destroy-bindings", unbinder = (record) =>
if record && model.eql(record)
model.constructor.unbind(event,binder)
model.constructor.unbind("destroy-bindings", unbinder)
)

get: (item,value) ->
if typeof item[value] is "function"
result = item[value]()
Expand All @@ -22,15 +31,11 @@ class Update extends Template

bind: (operators,model,el,options) ->
el.bind("change", => @change(operators,model,el,options))

for operator in operators
event = if options.watch then "update["+operator.property+"]" else "change"
model.constructor.bind(event, binder = () => @update([operator],model,el,options))
model.constructor.bind("destroy-bindings", unbinder = (record) =>
if record && model.eql(record)
model.constructor.unbind(event,binder)
model.constructor.unbind("destroy-bindings", unbinder)
)

if options.watch
@init([operator],model,el,options,"update["+operator.property+"]") for operator in operators
else
@init(operators,model,el,options,"change")

@update(operators,model,el,options)

Expand Down Expand Up @@ -79,31 +84,11 @@ class Options extends Template
ops = operators.filter((e) -> e.name is "options")[0]
opsSelected = operators.filter((e) -> e.name is "selectedOptions")[0]

# ops
opsEvent = "update["+ops.property+"]"
model.constructor.bind(opsEvent, opsUpdateBinder = () => @update([ops,opsSelected],model,el,options))
model.constructor.bind("destroy-bindings", opsUpdateUnbinder = (record) =>
if record && model.eql(record)
console.log("unbind " + opsEvent)
model.constructor.unbind(opsEvent,opsUpdateBinder)
model.constructor.unbind("destroy-bindings", opsUpdateUnbinder)
)

# opsSelected
opsSelectedEvent = "update["+opsSelected.property+"]"
model.constructor.bind(opsSelectedEvent, opsSelectedBinder = () => @update([ops,opsSelected],model,el,options))
model.constructor.bind("destroy-bindings", opsSelectedUnbinder = (record) =>
if record && model.eql(record)
model.constructor.unbind(opsSelectedEvent,opsSelectedBinder)
model.constructor.unbind("destroy-bindings", opsSelectedUnbinder)
)
# ops
@init([ops,opsSelected],model,el,options,"update["+ops.property+"]")
@init([ops,opsSelected],model,el,options,"update["+opsSelected.property+"]")
else
model.constructor.bind("update", binder = () => @update(operators,model,el,options))
model.constructor.bind("destroy-bindings", unbinder = (record) =>
if record && model.eql(record)
model.constructor.unbind("update",binder)
model.constructor.unbind("destroy-bindings", unbinder)
)
@init(operators,model,el,options,"update")

@update(operators,model,el,options)

Expand Down Expand Up @@ -188,9 +173,10 @@ class Enable 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,"change")

@update(operators,model,el,options)

unbind: (operators,model,el,options) ->
Expand Down
81 changes: 29 additions & 52 deletions src/spine.databind.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 00c2be7

Please sign in to comment.