Skip to content

Commit

Permalink
add Binding.parseFormatterArguments for code reuse, fix failing forma…
Browse files Browse the repository at this point in the history
…tter.publish test
  • Loading branch information
benadamstyles committed Aug 8, 2016
1 parent a898adf commit 32fd38b
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/bindings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ class Rivets.Binding
@observer = @observe @view.models, @keypath, @sync
@model = @observer.target

parseFormatterArguments: (args, formatterIndex) =>
args = (Rivets.TypeParser.parse(arg) for arg in args)
processedArgs = []

for arg, ai in args
processedArgs.push if arg.type is Rivets.TypeParser.types.primitive
arg.value
else
@formatterObservers[formatterIndex] or= {}

unless observer = @formatterObservers[formatterIndex][ai]
observer = @observe @view.models, arg.value, @sync
@formatterObservers[formatterIndex][ai] = observer

observer.value()

processedArgs

# Applies all the current formatters to the supplied value and returns the
# formatted value.
formattedValue: (value) =>
Expand All @@ -49,20 +67,7 @@ class Rivets.Binding
id = args.shift()
formatter = @view.formatters[id]

args = (Rivets.TypeParser.parse(arg) for arg in args)
processedArgs = []

for arg, ai in args
processedArgs.push if arg.type is 0
arg.value
else
@formatterObservers[fi] or= {}

unless observer = @formatterObservers[fi][ai]
observer = @observe @view.models, arg.value, @sync
@formatterObservers[fi][ai] = observer

observer.value()
processedArgs = @parseFormatterArguments args, fi

if formatter?.read instanceof Function
value = formatter.read.call @model, value, processedArgs...
Expand Down Expand Up @@ -108,12 +113,14 @@ class Rivets.Binding
if @observer
value = @getValue @el

for formatter in @formatters.slice(0).reverse()
for formatter, fi in @formatters.slice(0).reverse()
args = formatter.split /\s+/
id = args.shift()

processedArgs = @parseFormatterArguments args, fi

if @view.formatters[id]?.publish
value = @view.formatters[id].publish value, args...
value = @view.formatters[id].publish value, processedArgs...

@observer.setValue value

Expand Down

0 comments on commit 32fd38b

Please sign in to comment.