DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • Show and Hide Elements
  • / On this page
    • Show and Hide Elements

      page
      • source

      Instead of showing and hiding elements by changing the DOM directly like:

      $("h1").show()
      $("h1").hide()
      

      Make the template show or hide those elements when a value changes.

      {{#if visible}}
        <h1>{{message}}</h1>
      {{/if}}
      

      When the button is clicked, change the observable value.

      data.attr("visible", !data.attr("visible"))
      

      Application State

      Typically, it’s not a good idea to mix view state and application data. In the previous example, the message is application data, while the visible property represents view state. In CanJS, state and data should be separated using different observables.

      var data = new can.Map({message: "Hello World!"}),
          state = new can.Map({visible: true});
      
      var frag = can.view("app-template", {
        data: data,
        state: state
      });
      

      As an application gets more complex, separating state from data makes things more maintainable.

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