e682703a by Adam Heath

Reduce minified version by 219 bytes.

1 parent 23aa3d90
...@@ -15,21 +15,23 @@ class Rivets.Binding ...@@ -15,21 +15,23 @@ class Rivets.Binding
15 # element, the type of binding, the model object and the keypath at which 15 # element, the type of binding, the model object and the keypath at which
16 # to listen for changes. 16 # to listen for changes.
17 constructor: (@el, @type, @model, @keypath, @options = {}) -> 17 constructor: (@el, @type, @model, @keypath, @options = {}) ->
18 unless @binder = Rivets.binders[type] 18 unless binder = Rivets.binders[type]
19 for identifier, value of Rivets.binders 19 for identifier, value of Rivets.binders
20 if identifier isnt '*' and identifier.indexOf('*') isnt -1 20 if identifier isnt '*' and identifier.indexOf('*') isnt -1
21 regexp = new RegExp "^#{identifier.replace('*', '.+')}$" 21 regexp = new RegExp "^#{identifier.replace('*', '.+')}$"
22 if regexp.test type 22 if regexp.test type
23 @binder = value 23 binder = value
24 @args = new RegExp("^#{identifier.replace('*', '(.+)')}$").exec type 24 args = new RegExp("^#{identifier.replace('*', '(.+)')}$").exec type
25 @args.shift() 25 args.shift()
26 @args = args
26 27
27 @binder or= Rivets.binders['*'] 28 binder or= Rivets.binders['*']
28 29
29 if @binder instanceof Function 30 if binder instanceof Function
30 @binder = {routine: @binder} 31 binder = {routine: binder}
31 32
32 @formatters = @options.formatters || [] 33 @binder = binder
34 @formatters = options.formatters || []
33 35
34 # Applies all the current formatters to the supplied value and returns the 36 # Applies all the current formatters to the supplied value and returns the
35 # formatted value. 37 # formatted value.
...@@ -54,19 +56,21 @@ class Rivets.Binding ...@@ -54,19 +56,21 @@ class Rivets.Binding
54 # Sets the value for the binding. This Basically just runs the binding routine 56 # Sets the value for the binding. This Basically just runs the binding routine
55 # with the suplied value formatted. 57 # with the suplied value formatted.
56 set: (value) => 58 set: (value) =>
57 value = if value instanceof Function and !@binder.function 59 binder = @binder
58 @formattedValue value.call @model, @options.bindContext 60 value = @formattedValue if value instanceof Function and !binder.function
61 value.call @model, @options.bindContext
59 else 62 else
60 @formattedValue value 63 value
61 64
62 @binder.routine?.call @, @el, value 65 binder.routine?.call @, @el, value
63 66
64 # Syncs up the view binding with the model. 67 # Syncs up the view binding with the model.
65 sync: => 68 sync: =>
69 keypath = @keypath
66 @set if @options.bypass 70 @set if @options.bypass
67 @model[@keypath] 71 @model[keypath]
68 else 72 else
69 Rivets.config.adapter.read @model, @keypath 73 Rivets.config.adapter.read @model, keypath
70 74
71 # Publishes the value currently set on the input element back to the model. 75 # Publishes the value currently set on the input element back to the model.
72 publish: => 76 publish: =>
...@@ -76,8 +80,9 @@ class Rivets.Binding ...@@ -76,8 +80,9 @@ class Rivets.Binding
76 args = formatter.split /\s+/ 80 args = formatter.split /\s+/
77 id = args.shift() 81 id = args.shift()
78 82
79 if Rivets.formatters[id]?.publish 83 f = Rivets.formatters[id]
80 value = Rivets.formatters[id].publish value, args... 84 if f?.publish
85 value = f.publish value, args...
81 86
82 Rivets.config.adapter.publish @model, @keypath, value 87 Rivets.config.adapter.publish @model, @keypath, value
83 88
...@@ -169,15 +174,15 @@ defaultExpressionParser = (view, node, type, models, value) -> ...@@ -169,15 +174,15 @@ defaultExpressionParser = (view, node, type, models, value) ->
169 context = (ctx.trim() for ctx in pipes.shift().split '<') 174 context = (ctx.trim() for ctx in pipes.shift().split '<')
170 path = context.shift() 175 path = context.shift()
171 splitPath = path.split /\.|:/ 176 splitPath = path.split /\.|:/
172 options = {} 177 options =
173 options.formatters = pipes 178 formatters: pipes
174 options.bypass = path.indexOf(':') != -1 179 bypass: path.indexOf(':') != -1
175 options.bindContext = models 180 bindContext: models
176 if splitPath[0] 181 firstPart = splitPath.shift()
177 model = models[splitPath.shift()] 182 model = if firstPart
183 models[firstPart]
178 else 184 else
179 model = models 185 models
180 splitPath.shift()
181 keypath = splitPath.join '.' 186 keypath = splitPath.join '.'
182 187
183 if model 188 if model
...@@ -193,8 +198,8 @@ defaultExpressionParser = (view, node, type, models, value) -> ...@@ -193,8 +198,8 @@ defaultExpressionParser = (view, node, type, models, value) ->
193 class Rivets.View 198 class Rivets.View
194 # The DOM elements and the model objects for binding are passed into the 199 # The DOM elements and the model objects for binding are passed into the
195 # constructor. 200 # constructor.
196 constructor: (@els, @models) -> 201 constructor: (els, @models) ->
197 @els = [@els] unless (@els.jquery || @els instanceof Array) 202 @els = if (els.jquery || els instanceof Array) then els else [els]
198 @build() 203 @build()
199 204
200 # Regular expression used to match binding attributes. 205 # Regular expression used to match binding attributes.
...@@ -206,7 +211,7 @@ class Rivets.View ...@@ -206,7 +211,7 @@ class Rivets.View
206 # binding declaration. Subsequent calls to build will replace the previous 211 # binding declaration. Subsequent calls to build will replace the previous
207 # Rivets.Binding instances with new ones, so be sure to unbind them first. 212 # Rivets.Binding instances with new ones, so be sure to unbind them first.
208 build: => 213 build: =>
209 @bindings = [] 214 bindings = @bindings = []
210 skipNodes = [] 215 skipNodes = []
211 bindingRegExp = @bindingRegExp() 216 bindingRegExp = @bindingRegExp()
212 217
...@@ -234,7 +239,7 @@ class Rivets.View ...@@ -234,7 +239,7 @@ class Rivets.View
234 binding = defaultExpressionParser @, node, type, @models, attribute.value 239 binding = defaultExpressionParser @, node, type, @models, attribute.value
235 240
236 if binding 241 if binding
237 @bindings.push binding 242 bindings.push binding
238 243
239 attributes = null if attributes 244 attributes = null if attributes
240 245
...@@ -326,10 +331,10 @@ Rivets.binders = ...@@ -326,10 +331,10 @@ Rivets.binders =
326 unbind: (el) -> 331 unbind: (el) ->
327 unbindEvent el, 'change', @currentListener 332 unbindEvent el, 'change', @currentListener
328 routine: (el, value) -> 333 routine: (el, value) ->
329 if el.type is 'radio' 334 el.checked = if el.type is 'radio'
330 el.checked = el.value is value 335 el.value is value
331 else 336 else
332 el.checked = !!value 337 !!value
333 338
334 unchecked: 339 unchecked:
335 publishes: true 340 publishes: true
...@@ -338,10 +343,10 @@ Rivets.binders = ...@@ -338,10 +343,10 @@ Rivets.binders =
338 unbind: (el) -> 343 unbind: (el) ->
339 unbindEvent el, 'change', @currentListener 344 unbindEvent el, 'change', @currentListener
340 routine: (el, value) -> 345 routine: (el, value) ->
341 if el.type is 'radio' 346 el.checked = if el.type is 'radio'
342 el.checked = el.value isnt value 347 el.value isnt value
343 else 348 else
344 el.checked = !value 349 !value
345 350
346 show: (el, value) -> 351 show: (el, value) ->
347 el.style.display = if value then '' else 'none' 352 el.style.display = if value then '' else 'none'
...@@ -365,10 +370,11 @@ Rivets.binders = ...@@ -365,10 +370,11 @@ Rivets.binders =
365 el.value = if value? then value else '' 370 el.value = if value? then value else ''
366 371
367 text: (el, value) -> 372 text: (el, value) ->
373 newValue = if value? then value else ''
368 if el.innerText? 374 if el.innerText?
369 el.innerText = if value? then value else '' 375 el.innerText = newValue
370 else 376 else
371 el.textContent = if value? then value else '' 377 el.textContent = newValue
372 378
373 "on-*": 379 "on-*":
374 function: true 380 function: true
...@@ -383,29 +389,32 @@ Rivets.binders = ...@@ -383,29 +389,32 @@ Rivets.binders =
383 bind: (el, collection) -> 389 bind: (el, collection) ->
384 el.removeAttribute ['data', rivets.config.prefix, @type].join('-').replace '--', '-' 390 el.removeAttribute ['data', rivets.config.prefix, @type].join('-').replace '--', '-'
385 routine: (el, collection) -> 391 routine: (el, collection) ->
386 if @iterated? 392 iterated = @iterated
387 for view in @iterated 393 if iterated?
394 for view in iterated
388 view.unbind() 395 view.unbind()
389 e.parentNode.removeChild e for e in view.els 396 e.parentNode.removeChild e for e in view.els
390 else 397 else
391 @marker = document.createComment " rivets: #{@type} " 398 marker = @marker = document.createComment " rivets: #{@type} "
392 el.parentNode.insertBefore @marker, el 399 parentNode = el.parentNode
393 el.parentNode.removeChild el 400 parentNode.insertBefore marker, el
401 parentNode.removeChild el
394 402
395 @iterated = iterated = [] 403 @iterated = iterated = []
396 404
397 if collection 405 if collection
406 marker = @marker
398 iterate collection, (item, i) => 407 iterate collection, (item, i) =>
399 data = {} 408 data = {}
400 data[n] = m for n, m of @view.models 409 data[n] = m for n, m of @view.models
401 data[@args[0]] = item 410 data[@args[0]] = item
402 data["#{@args[0]}_index"] = data['rivets_index'] = i 411 data["#{@args[0]}_index"] = data['rivets_index'] = i
403 itemEl = el.cloneNode true 412 itemEl = el.cloneNode true
404 if iterated.length > 0 413 previous = if iterated.length > 0
405 previous = iterated[iterated.length - 1].els[0] 414 iterated[iterated.length - 1].els[0]
406 else 415 else
407 previous = @marker 416 marker
408 @marker.parentNode.insertBefore itemEl, previous.nextSibling ? null 417 marker.parentNode.insertBefore itemEl, previous.nextSibling ? null
409 iterated.push rivets.bind itemEl, data 418 iterated.push rivets.bind itemEl, data
410 419
411 "class-*": (el, value) -> 420 "class-*": (el, value) ->
......