DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
    • can-define
    • can-define/list/list
    • can-define/map/map
    • can-route
    • can-route-pushstate
    • can-set
    • can-stache
      • Pages
        • Magic Tag Types
        • Scope and Context
        • Expressions
        • Template Acquisition
        • Helpers
        • Live Binding
      • Methods
        • from
        • registerConverter
        • registerHelper
        • registerPartial
        • registerSimpleHelper
        • safeString
      • Tags
        • {{expression}}
        • {{{expression}}}
        • {{#expression}}
        • {{/expression}}
        • {{^expression}}
        • {{>key}}
        • {{!expression}}
        • {{else}}
      • Expressions
        • Bracket Expression
        • Call Expression
        • Hash Expression
        • Helper Expression
        • KeyLookup Expression
        • Literal Expression
      • Key Operators
        • @at
        • ~compute
        • ./current
        • ../parent
        • %special
        • this
        • *variable
        • key
      • Helpers
        • {{#if expression}}
        • {{#unless expression}}
        • {{#each expression}}
        • {{#with expression}}
        • {{log}}
        • {{#is expressions}}
        • {{#switch expression}}
        • {{#case expression}}
        • {{#default}}
        • {{joinBase expressions}}
      • Types
        • getterSetter
        • helper
        • helperOptions
        • renderer
        • sectionRenderer
        • simpleHelper
    • can-stache/helpers/route
    • can-stache-bindings
  • Ecosystem
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Core
  • /
  • can-stache
  • /
  • Live Binding
  • / On this page
    • Live Binding

      page

      Live binding refers to templates which update themselves when their state or data changes. can-stache templates are able to listen to observables (like can-define/map/map, can-define/list/list, and can-compute) changing and update the page to reflect those changes.

      • source

      Live binding lets you focus on changing data and state without having to worry about also updating the DOM to reflect those changes.

      In this example, we have a simple user welcome screen.

      <h1>Welcome {{user}}!</h1>
      <p>
          {{#if messages}}
              You have {{messages}} new messages.
          {{else}}
              You no messages.
          {{/messages}}
      </p>
      
      var data = new DefineMap({
          user: 'Tina Fey',
          messages: 0
      });
      
      var template = stache( document.getElementById("template").innerHTML );
      var frag = template( data );
      document.body.appendChild( frag );
      

      The template evaluates the messages and adds the hooks for live binding automatically. Since we have no message it will render:

      <h1>Welcome Tina Fey!</h1>
      <p>You no messages.</p>
      

      Now say we have a request that updates the messages attribute to have 5 messages.

      data.message = 5;
      

      After the template receives this update, it will automatically update the paragraph tag to reflect the new value.

      <p>You have 5 new message.</p>
      

      For more information visit the can-define/map/map documentation.

      Binding between components

      If you are looking for information on bindings between components like this:

      (event)="key()" for event binding.
      {prop}="key" for one-way binding to a child.
      {^prop}="key" for one-way binding to a parent.
      {(prop)}="key" for two-way binding.
      

      See can-stache-bindings.

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.