Abstract out the binder lookup code from the Binding constructor.
Showing
1 changed file
with
18 additions
and
15 deletions
... | @@ -9,6 +9,22 @@ Rivets = {} | ... | @@ -9,6 +9,22 @@ 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 | findBinder = (type) -> | ||
13 | unless binder = Rivets.binders[type] | ||
14 | binder = Rivets.binders['*'] | ||
15 | for identifier, value of Rivets.binders | ||
16 | if identifier isnt '*' and identifier.indexOf('*') isnt -1 | ||
17 | regexp = new RegExp "^#{identifier.replace('*', '(.+)')}$" | ||
18 | if regexp.test type | ||
19 | binder = value | ||
20 | args = regexp.exec type | ||
21 | args.shift() | ||
22 | |||
23 | if binder instanceof Function | ||
24 | binder = {routine: binder} | ||
25 | |||
26 | [binder, args] | ||
27 | |||
12 | # A single binding between a model attribute and a DOM element. | 28 | # A single binding between a model attribute and a DOM element. |
13 | class Rivets.Binding | 29 | class Rivets.Binding |
14 | # All information about the binding is passed into the constructor; the DOM | 30 | # All information about the binding is passed into the constructor; the DOM |
... | @@ -16,21 +32,8 @@ class Rivets.Binding | ... | @@ -16,21 +32,8 @@ class Rivets.Binding |
16 | # to listen for changes. | 32 | # to listen for changes. |
17 | constructor: (@el, @type, @model, @keypath, options) -> | 33 | constructor: (@el, @type, @model, @keypath, options) -> |
18 | @options = (options ||= {}) | 34 | @options = (options ||= {}) |
19 | unless binder = Rivets.binders[type] | 35 | |
20 | binder = Rivets.binders['*'] | 36 | [@binder, @args] = findBinder(type) |
21 | for identifier, value of Rivets.binders | ||
22 | if identifier isnt '*' and identifier.indexOf('*') isnt -1 | ||
23 | regexp = new RegExp "^#{identifier.replace('*', '(.+)')}$" | ||
24 | if regexp.test type | ||
25 | binder = value | ||
26 | args = regexp.exec type | ||
27 | args.shift() | ||
28 | @args = args | ||
29 | |||
30 | if binder instanceof Function | ||
31 | binder = {routine: binder} | ||
32 | |||
33 | @binder = binder | ||
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 | ... | ... |
-
Please register or sign in to post a comment