Add if and unless binders for binding and unbding entire subviews in a template. [Closes #173]
Showing
1 changed file
with
47 additions
and
0 deletions
... | @@ -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 | ... | ... |
-
Please register or sign in to post a comment