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
  • /
  • registerConverter
  • / On this page
    • registerConverter

      function

      Register a helper for bidirectional value conversion.

      • source

      stache.registerConverter(converterName, getterSetter)

      Creates a helper that can do two-way conversion between two values. This is especially useful with {($two-way)} bindings like:

      <input {($value)}='numberToString(~age)'/>
      

      A converter helper provides:

      • a get method that returns the value of the left value given the arguments passed on the right.
      • a set method that updates one or multiple of the right arguments computes given a new left value.

      numberToString might converts a number (age) to a string (value), and the string (value) to a number (age) as follows:

      stache.registerConverter("numberToString", {
       get: function(fooCompute) {
          return "" + fooCompute();
       },
       set: function(newVal, fooCompute) {
          fooCompute(+newVal);
       }
      });
      

      Parameters

      1. converterName {String}:

        The name of the converter helper.

      2. getterSetter {getterSetter}:

        An object containing get() and set() functions.

      Use

      These helpers are useful for avoiding creating can-define/map/map getters and setters that do similar conversions on the view model. Instead, a converter can keep your viewModels more ignorant of the demands of the view. Especially as the view's most common demand is that everything must be converted to a string.

      That being said, the following is a richer example of a converter, but one that should probably be part of a view model.

      <input {($value)}='fullName(~first, ~last)'/>
      

      The following might converts both ways first and last to value.

      var canBatch = require("can-event/batch/batch");
      
      stache.registerConverter("fullName", {
       get: function(first, last) {
          return first() + last();
       },
       set: function(newFullName, first, last) {
          canBatch.start();
          var parts = newFullName.split(" ");
          first(parts[0]);
          last(parts[1]);
          canBatch.stop();
       }
      });
      

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