f1a9565d by Adam Heath

Abstract out the binder lookup code from the Binding constructor.

1 parent 6ad92e53
...@@ -9,13 +9,7 @@ Rivets = {} ...@@ -9,13 +9,7 @@ Rivets = {}
9 # Polyfill For String::trim. 9 # Polyfill For String::trim.
10 unless String::trim then String::trim = -> @replace /^\s+|\s+$/g, '' 10 unless String::trim then String::trim = -> @replace /^\s+|\s+$/g, ''
11 11
12 # A single binding between a model attribute and a DOM element. 12 findBinder = (type) ->
13 class Rivets.Binding
14 # All information about the binding is passed into the constructor; the DOM
15 # element, the type of binding, the model object and the keypath at which
16 # to listen for changes.
17 constructor: (@el, @type, @model, @keypath, options) ->
18 @options = (options ||= {})
19 unless binder = Rivets.binders[type] 13 unless binder = Rivets.binders[type]
20 binder = Rivets.binders['*'] 14 binder = Rivets.binders['*']
21 for identifier, value of Rivets.binders 15 for identifier, value of Rivets.binders
...@@ -25,12 +19,21 @@ class Rivets.Binding ...@@ -25,12 +19,21 @@ class Rivets.Binding
25 binder = value 19 binder = value
26 args = regexp.exec type 20 args = regexp.exec type
27 args.shift() 21 args.shift()
28 @args = args
29 22
30 if binder instanceof Function 23 if binder instanceof Function
31 binder = {routine: binder} 24 binder = {routine: binder}
32 25
33 @binder = binder 26 [binder, args]
27
28 # A single binding between a model attribute and a DOM element.
29 class Rivets.Binding
30 # All information about the binding is passed into the constructor; the DOM
31 # element, the type of binding, the model object and the keypath at which
32 # to listen for changes.
33 constructor: (@el, @type, @model, @keypath, options) ->
34 @options = (options ||= {})
35
36 [@binder, @args] = findBinder(type)
34 @formatters = options.formatters || [] 37 @formatters = options.formatters || []
35 38
36 # Applies all the current formatters to the supplied value and returns the 39 # Applies all the current formatters to the supplied value and returns the
......