DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
        • Syntaxes
          • on:event
          • toChild:from
          • toParent:to
          • twoWay:bind
          • can-stache-bindings.legacy-syntaxes
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-stache-bindings

  • npm package badge
  • Star
  • Edit on GitHub

Provides template event, one-way bindings, and two-way bindings.

Use

The can-stache-bindings plugin provides useful custom attributes for template declarative event, one-way bindings, and two-way bindings on element attributes, component ViewModels, and the scope. Bindings look like:

  • on:event="key()" for event binding.
  • prop:from="key" for one-way binding to a child.
  • prop:to="key" for one-way binding to a parent.
  • prop:bind="key" for two-way binding.

Note: DOM attribute names are case-insensitive, but ViewModel or scope properties can be camelCase and stache will encode them so they work correctly in the DOM.

The following are the bindings that should be used with can-stache:

event

Binds to childEvent on <my-component>'s ViewModel and calls method on the scope with the specified arguments:

<my-component on:childEvent="method('primitive', key, hash1=key1)"/>

If the element does not have a ViewModel, binds to domEvent on the element and calls method on the scope with the specified arguments.

<div on:domEvent="method('primitive', key, hash1=key1)"/>

You can also explicitly listen to events on the ViewModel using on:vm:childEvent or on the element using on:el:domEvent.

one-way to child

Updates childProp in <my-component>’s ViewModel with value in the scope:

<my-component childProp:from="value"/>

This can be read as "set childProp from value".

If the element does not have a ViewModel, updates the child-attr attribute or property of the element with value in the scope:

<div child-attr:from="value"/>

You can also explicitly use the ViewModel using vm:childProp:from="value" or the element using el:child-attr:from="value".

Note: If value being passed to the component is an object, changes to the objects properties will still be visible to the component. Objects are passed by reference. See One Way Binding With Objects.

one-way to parent

Updates value in the scope with childProp in <my-component>’s ViewModel:

<my-component childProp:to="value"/>

This can be read as "send childProp to value".

If the element does not have a ViewModel, updates value in the scope with the child-attr attribute or property of the element.

<div child-attr:to="value"/>

You can also explicitly use the ViewModel using vm:childProp:to="value" or the element using el:child-attr:to="value".

Note: If value being passed to the component is an object, changes to the objects properties will still be visible to the component. Objects are passed by reference. See One Way Binding With Objects.

two-way

Updates childProp in <my-component>’s ViewModel with value in the scope and vice versa:

<my-component childProp:bind="value"/>

Updates the child-attr attribute or property of the element with value in the scope and vice versa:

<div child-attr:bind="value"/>

You can also explicitly use the ViewModel using vm:childProp:bind="value" or the element using el:child-attr:bind="value".

One Way Binding With Objects

childProp:from="key" (one-way to child) or child-prop:to="key" (one-way to parent) is used to pass values from the current scope to a component or vice versa, respectively.

Generally, this binding only observes changes in one direction, but when key is an object (POJO, DefineMap, etc), it is passed as a reference, behaving in much the same way as the following snippet.

function component(bar) {
    // changes to bar's properties are preserved
    bar.quux = 'barfoo';

    // but replacing bar is not
    bar = {
        quux: 'hello world'
    };
}

var foo = {
    quux: 'foobar'
};
component(foo);

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

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news