DoneJS StealJS jQuery++ FuncUnit DocumentJS
6.6.1
5.33.3 4.3.0 3.14.1 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-bind
      • can-compute
      • can-debug
      • can-deep-observable
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
        • define
          • extend
          • tag
          • view
          • ViewModel
        • create
          • <tag bindings...>
          • new Component
        • elements
          • <can-slot>
          • <can-template>
        • lifecycle hooks
          • connectedCallback
        • deprecated
          • beforeremove
          • <content>
          • events
          • helpers
          • leakScope
          • viewModel
      • can-observable-bindings
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-ndjson
      • can-connect-tag
      • can-define-realtime-rest-model
      • can-define-rest-model
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • 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

<tag bindings...>

  • Edit on GitHub

Create a component using HTML and can-stache-bindings.

<TAG BINDINGS...>[TEMPLATES][LIGHT_DOM]</TAG>

Create an instance of a component on a particular tag in a can-stache view. Use the bindings syntaxes to set up bindings.

The following creates a my-autocomplete element and passes the my-autocomplete’s ViewModel the Search model as its source property and a <can-template> that is used to render the search results:

<my-autocomplete source:from="this.Search">
    <can-template name="search-results">
        <li>{{name}}</li>
    </can-template>
</my-autocomplete>

Parameters

  1. TAG {String}:

    An HTML tag name that matches the tag property of the component. Tag names should include a hyphen (-) or a colon (:) like: acme-tabs or acme:tabs.

  2. BINDINGS {can-stache-bindings}:

    Use the following binding syntaxes to connect the component’s ViewModel to the view’s scope:

    • key:from=expression — one-way data binding to child
    • key:to=expression — one-way data binding to parent
    • key:bind=expression — two-way data binding child to parent
    • on:event=expression — event binding on the view model
  3. TEMPLATES {sectionRenderer(context, helpers)}:

    Between the starting and ending tag can exist one or many <can-template> elements. Use <can-template> elements to pass custom templates to child components. Each <can-template> is given a name attribute and can be rendered by a corresponding <can-slot> in the component’s view.

    For example, the following passes how each search result should look and an error message if the source is unable to request data:

    <my-autocomplete source:from="Search">
        <can-template name="search-results">
            <li>{{name}}</li>
        </can-template>
        <can-template name="search-error">
            <div class="error">{{message}}</div>
        </can-template>
    </my-autocomplete>
    

Use

To create a component instance, add its tag to your html page or a stache view. For example, the following has a <my-counter> element in the page:

<my-counter count:from="2"></my-counter>

<script type="module">
import {Component} from "can";

Component.extend( {
    tag: "my-counter",
    view: `
        Count: {{this.count}}.
        <button on:click="this.increment()">+1</button>
    `,
    ViewModel: {
        count: {default: 0},
        increment(){
            this.count++;
        }
    }
} );
</script>

When Component.extend defines the my-counter element, a component instance will be created. Creating a component instance involves creating a new ViewModel and rendering the view.

Differences between components in stache and HTML

Currently, components embedded in your pages HTML have limited abilities compared to those embedded in stache templates. Components embedded in HTML:

  • Must have a closing tag like <my-counter></my-counter>. Tags like <my-counter/> are allowed in stache.
  • Are currently unable to use <can-template> or pass light DOM to be used by <content>.
  • Do not have a scope, so they are unable to pass anything other than primitives. For example, <my-counter count:from="myNumber"></my-counter> will not be able to look up a myNumber.

Components embedded in your pages should usually be the root component and be used to create other components. For example, the following <my-app> component creates the <hello-world/> and <goodbye-moon/> elements:

<my-app></my-app>

<script type="module">
import {Component} from "can";

Component.extend({
    tag: "hello-world",
    view: `Hello World!`
});

Component.extend({
    tag: "goodbye-moon",
    view: `Goodbye Moon.`
})


Component.extend( {
    tag: "my-app",
    view: `
        <p><hello-world/></p>
        <p><goodbye-moon/></p>
    `
} );
</script>

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

On this page

Get help

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