b775faa8 by Michael Richards

Merge pull request #176 from mikeric/if-unless-binders

Add if and unless binders
2 parents a85a0881 655ee9dc
...@@ -361,6 +361,53 @@ Rivets.binders = ...@@ -361,6 +361,53 @@ Rivets.binders =
361 else 361 else
362 el.textContent = if value? then value else '' 362 el.textContent = if value? then value else ''
363 363
364 if:
365 block: true
366
367 bind: (el) ->
368 unless @marker?
369 attr = ['data', @view.config.prefix, @type].join('-').replace '--', '-'
370 declaration = el.getAttribute attr
371
372 @marker = document.createComment " rivets: #{@type} #{declaration} "
373
374 el.removeAttribute attr
375 el.parentNode.insertBefore @marker, el
376 el.parentNode.removeChild el
377
378 unbind: ->
379 @nested?.unbind()
380
381 routine: (el, value) ->
382 if value is not @nested?
383 if value
384 models = {}
385 models[key] = model for key, model of @view.models
386
387 options =
388 binders: @view.options.binders
389 formatters: @view.options.formatters
390 config: @view.options.config
391
392 (@nested = new Rivets.View(el, models, options)).bind()
393 @marker.parentNode.insertBefore el, @marker.nextSibling
394 else
395 el.parentNode.removeChild el
396 @nested.unbind()
397 delete @nested
398
399 unless:
400 block: true
401
402 bind: (el) ->
403 rivets.binders.if.bind.call @, el
404
405 unbind: ->
406 rivets.binders.if.unbind.call @
407
408 routine: (el, value) ->
409 rivets.binders.if.routine.call @, el, not value
410
364 "on-*": 411 "on-*":
365 function: true 412 function: true
366 413
......