c1e29df4 by Adam Heath

Optimize several parts, to reduce minified version; gives a savings of

242 bytes.
1 parent 38c357c6
...@@ -34,12 +34,13 @@ class Rivets.Binding ...@@ -34,12 +34,13 @@ class Rivets.Binding
34 # Applies all the current formatters to the supplied value and returns the 34 # Applies all the current formatters to the supplied value and returns the
35 # formatted value. 35 # formatted value.
36 formattedValue: (value) => 36 formattedValue: (value) =>
37 model = @model
37 for formatter in @formatters 38 for formatter in @formatters
38 args = formatter.split /\s+/ 39 args = formatter.split /\s+/
39 id = args.shift() 40 id = args.shift()
40 41
41 formatter = if @model[id] instanceof Function 42 formatter = if model[id] instanceof Function
42 @model[id] 43 model[id]
43 else 44 else
44 Rivets.formatters[id] 45 Rivets.formatters[id]
45 46
...@@ -92,17 +93,7 @@ class Rivets.Binding ...@@ -92,17 +93,7 @@ class Rivets.Binding
92 Rivets.config.adapter.subscribe @model, @keypath, @sync 93 Rivets.config.adapter.subscribe @model, @keypath, @sync
93 @sync() if Rivets.config.preloadData 94 @sync() if Rivets.config.preloadData
94 95
95 if @options.dependencies?.length 96 loopDeps @, (model, keypath) => Rivets.config.adapter.subscribe model, keypath, @sync
96 for dependency in @options.dependencies
97 if /^\./.test dependency
98 model = @model
99 keypath = dependency.substr 1
100 else
101 dependency = dependency.split '.'
102 model = @view.models[dependency.shift()]
103 keypath = dependency.join '.'
104
105 Rivets.config.adapter.subscribe model, keypath, @sync
106 97
107 # Unsubscribes from the model and the element. 98 # Unsubscribes from the model and the element.
108 unbind: => 99 unbind: =>
...@@ -111,17 +102,20 @@ class Rivets.Binding ...@@ -111,17 +102,20 @@ class Rivets.Binding
111 unless @options.bypass 102 unless @options.bypass
112 Rivets.config.adapter.unsubscribe @model, @keypath, @sync 103 Rivets.config.adapter.unsubscribe @model, @keypath, @sync
113 104
114 if @options.dependencies?.length 105 loopDeps @, (model, keypath) => Rivets.config.adapter.unsubscribe model, keypath, @sync
115 for dependency in @options.dependencies 106
107 loopDeps = (binder, callback) ->
108 if binder.options.dependencies?.length
109 for dependency in binder.options.dependencies
116 if /^\./.test dependency 110 if /^\./.test dependency
117 model = @model 111 model = binder.model
118 keypath = dependency.substr 1 112 keypath = dependency.substr 1
119 else 113 else
120 dependency = dependency.split '.' 114 dependency = dependency.split '.'
121 model = @view.models[dependency.shift()] 115 model = binder.view.models[dependency.shift()]
122 keypath = dependency.join '.' 116 keypath = dependency.join '.'
123 117
124 Rivets.config.adapter.unsubscribe model, keypath, @sync 118 callback model, keypath
125 119
126 # A collection of bindings built from a set of parent elements. 120 # A collection of bindings built from a set of parent elements.
127 class Rivets.View 121 class Rivets.View
...@@ -321,8 +315,10 @@ Rivets.binders = ...@@ -321,8 +315,10 @@ Rivets.binders =
321 "on-*": 315 "on-*":
322 function: true 316 function: true
323 routine: (el, value) -> 317 routine: (el, value) ->
324 unbindEvent el, @args[0], @currentListener if @currentListener 318 firstArg = @args[0]
325 @currentListener = bindEvent el, @args[0], value, @model, @options.bindContext 319 currentListener = @currentListener
320 unbindEvent el, firstArg, currentListener if currentListener
321 @currentListener = bindEvent el, firstArg, value, @model, @options.bindContext
326 322
327 "each-*": 323 "each-*":
328 block: true 324 block: true
...@@ -338,7 +334,7 @@ Rivets.binders = ...@@ -338,7 +334,7 @@ Rivets.binders =
338 el.parentNode.insertBefore @marker, el 334 el.parentNode.insertBefore @marker, el
339 el.parentNode.removeChild el 335 el.parentNode.removeChild el
340 336
341 @iterated = [] 337 @iterated = iterated = []
342 338
343 if collection 339 if collection
344 for item in collection 340 for item in collection
...@@ -346,12 +342,12 @@ Rivets.binders = ...@@ -346,12 +342,12 @@ Rivets.binders =
346 data[n] = m for n, m of @view.models 342 data[n] = m for n, m of @view.models
347 data[@args[0]] = item 343 data[@args[0]] = item
348 itemEl = el.cloneNode true 344 itemEl = el.cloneNode true
349 if @iterated.length > 0 345 if iterated.length > 0
350 previous = @iterated[@iterated.length - 1].els[0] 346 previous = iterated[iterated.length - 1].els[0]
351 else 347 else
352 previous = @marker 348 previous = @marker
353 @marker.parentNode.insertBefore itemEl, previous.nextSibling ? null 349 @marker.parentNode.insertBefore itemEl, previous.nextSibling ? null
354 @iterated.push rivets.bind itemEl, data 350 iterated.push rivets.bind itemEl, data
355 351
356 "class-*": (el, value) -> 352 "class-*": (el, value) ->
357 elClass = " #{el.className} " 353 elClass = " #{el.className} "
......