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
  • /
  • string-to-any
  • / On this page
    • string-to-any

      function

      A converter that binds a value to a primitive value, two way converted back to that primitive value when the attribute changes.

      • source

      string-to-any(~item)

      When the getter is called, gets the value of the compute and calls .toString() on that value.

      When the setter is called, takes the new value and converts it to the primitive value using string-to-any and sets the compute using that converted value.

      <select {($value)}="string-to-any(~favePlayer)">
        <option value="23">Michael Jordan</option>
          <option value="32">Magic Johnson</option>
      </select>
      

      Parameters

      1. item {can-compute}:

        A compute holding a primitive value.

      Returns

      {can-compute}:

      A compute that will be used by can-stache-bindings as a getter/setter when the element's value changes.

      Use

      This is usually used with <select>s where you would like to two-way bind a string to a primitive value.

      <select {($value)}="string-to-any(~someValue)">
        <option value="2">Number</option>
        <option value="null">Null</option>
        <option value="foo">String</option>
        <option value="true">Boolean</option>
        <option value="NaN">NaN</option>
        <option value="Infinity">Infinity</option>
      </select>
      
      var str = document.getElementById('select-template').innerHTML;
      var template = stache(str);
      
      var map = new DefineMap({
          someValue: "foo"
      });
      
      document.body.appendChild(template(map));
      
      map.item = NaN; // select.value becomes "NaN"
      
      // Click the select box and choose Boolean
      map.item === true; // -> true
      

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