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

Commit

Permalink
Fixed options binder so it would init ops/opsSelected properly for wa…
Browse files Browse the repository at this point in the history
…tch callbacks

Updated options so you can specify only selectedOptions and it will just pull the values currently in the drop down
  • Loading branch information
nathanpalmer committed Aug 19, 2012
1 parent a01ade7 commit 9dab8a7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
2 changes: 0 additions & 2 deletions spec/SpineDataBindSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,6 @@ describe("Spine.DataBind", function() {

Tests();
});

});

describe("Click", function() {
Expand Down Expand Up @@ -1252,6 +1251,5 @@ describe("Spine.DataBind", function() {

Tests();
});

});
});
23 changes: 13 additions & 10 deletions src/spine.databind.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ class Options extends Template

bind: (operators,model,controller,el,options) ->
if options.watch
ops = operators.filter((e) -> e.name is "options")[0]
opsSelected = operators.filter((e) -> e.name is "selectedOptions")[0]
ops = operators.filter((e) -> e.name is "options")
opsSelected = operators.filter((e) -> e.name is "selectedOptions")
together = ops.concat(opsSelected)

# ops
@init([ops,opsSelected],model,controller,el,options,"update["+ops.property+"]")
@init([ops,opsSelected],model,controller,el,options,"update["+opsSelected.property+"]")
@init(together,model,controller,el,options,"update["+ops[0].property+"]") if ops and ops.length is 1
@init(together,model,controller,el,options,"update["+opsSelected[0].property+"]") if opsSelected and opsSelected.length is 1
else
@init(operators,model,controller,el,options,"update")

Expand All @@ -101,13 +102,17 @@ class Options extends Template
@update(operators,model,controller,el,options)

update: (operators,model,controller,el,options) ->
ops = operators.filter((e) -> e.name is "options")[0]
ops = operators.filter((e) -> e.name is "options")
opsSelected = operators.filter((e) -> e.name is "selectedOptions")
selectedOptions = if opsSelected.length is 1 then @get(model,opsSelected[0].property) else []
selectedOptions = [selectedOptions] if not (selectedOptions instanceof Array)

process = (array) ->
options = el.children('option')

if array instanceof Array
if not array
result = el.find("option").map((index,item) -> return { text: $(item).text(), value: $(item).val() })
else if array instanceof Array
result = ({ text: item, value: item} for item in array)
else
result = Object.keys(array)
Expand All @@ -122,7 +127,7 @@ class Options extends Template
)

count = 0
count = count = count + 1 for own property of result
count = count = count + 1 for own property in result
changed = false

for item,index in result
Expand Down Expand Up @@ -155,9 +160,7 @@ class Options extends Template

el.trigger("change") if changed

selectedOptions = if opsSelected.length is 1 then @get(model,opsSelected[0].property) else []
selectedOptions = [selectedOptions] if not (selectedOptions instanceof Array)
array = if ops then @get(model,ops.property,process) or [] else []
array = if ops.length is 1 then @get(model,ops[0].property,process) else null

process(array) if typeof array isnt "function"

Expand Down
46 changes: 29 additions & 17 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 9dab8a7

Please sign in to comment.