DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
    • can-construct-super
    • can-define-stream
    • can-fixture
    • can-fixture-socket
    • can-jquery
    • can-stache-converters
      • Pages
        • select[multiple]
        • input[type=checkbox]
        • input[type=radio]
        • select
      • Converters
        • boolean-to-inList
        • either-or
        • equal
        • index-to-selected
        • not
        • string-to-any
    • can-stream
    • can-vdom
    • can-view-import
    • can-zone
    • steal-stache
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Ecosystem
  • /
  • can-stache-converters
  • /
  • either-or
  • / On this page
    • either-or

      function

      a [can-stache-registerConverter converter] that two-way binds to a checkbox two values provided as arguments. This converter is useful when you have a binary decision that your user will implicitly understand.

      • source

      either-or(~chosen, a, b)

      When the getter is called, gets the value of the chosen compute and if it is equal to a returns true, otherwise it returns false.

      When the setter is called, if the new value is truthy, sets the chosen can-compute to a's value, otherwise sets it to b's value.

      <span>Favorite superhero:</span>
      <input type="checkbox" {($checked)}="either-or(~chosen, 'Batman', 'Superman')"> Batman?
      

      Parameters

      1. chosen {can-compute}:

        A compute where the chosen value (between a and b is stored). When the setter is called, this compute's value will be updated.

      2. a {*}:

        The true value. If the checkbox is checked, then a's value will be stored in the chosen compute.

      3. b {*}:

        The false value. If the checkbox is unchecked, then b's value will be stored in the chosen compute.

      Returns

      {can-compute}:

      A compute that will be used by can-stache-bindings as a getter/setter bound to the element.

      Use

      either-or is made to be used with <input type=checkbox> elements when there is a binary decision that can be made (so that multiple radio buttons are not needed).

      You pass 3 arguments to this converter. The first argument is a compute that represents the chosen value. The second argument is the default, truthy, value. And the third argument is the falsey value.

      <p>
          <input type="checkbox"
              ({$checked})="either-or(~pref, 'Star Trek', 'Star Wars')" />
          <span>Star Trek</span>
      </p>
      
      <p>Your fandom: {{pref}}</p>
      
      var template = stache.from("demo-template");
      
      var fan = new DefineMap({
          pref: "Star Trek"
      });
      
      document.body.appendChild(template(fan));
      
      // User unchecks the checkbox
      fan.pref === "Star Wars";
      
      // Changing the value in code:
      fan.pref === "Star Trek";
      
      // Checkbox is now checked again.
      

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