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
        • static
          • extend
        • prototype
          • ViewModel
          • events
          • helpers
          • leakScope
          • tag
          • view
          • viewModel
        • elements
          • <can-slot>
          • <can-template>
          • <content>
        • special events
          • beforeremove
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • 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

view

  • Edit on GitHub

Provides a view to render directly within the component’s element. The view is rendered with the component’s ViewModel instance. <content/> elements within the view are replaced by the source elements within the component’s tag.

renderer(data, helpers, nodeList)

A renderer returned by can-stache. For example:

Component({
  tag: "my-tabs",
  view: stache("<ul>{{#panels}}<li>{{title}}</li> ...")
});

Use

The view specified by the view property works similar to the W3C Shadow DOM proposal. It represents the contents of a custom element, while being able to reposition the user provided source elements with the tag.

There are three things to understand about a can-component’s view:

  • It is inserted into the component’s tag.
  • It is rendered with access to the component instance’s viewModel.
  • Any <content> tags within the view act as insertion points for the source elements.

The following example demonstrates all three features:

The following explains how each part works:

Component:

Component({
    tag: "my-greeting",
    view: stache("<h1><content/> - {{title}}</h1>"),
    ViewModel: DefineMap.extend({
        title: {
            value: "can-component"
        }
    })
});

This registers a component for elements like <my-greeting>. Its view will place an <h1> element directly within <my-greeting> and put the original contents of <my-greeting> within the beginning of <h1>. The component’s ViewModel adds a title value.

Source view:

<header>
  <my-greeting>
     {{site}}
  </my-greeting>
</header>

The source view is the view that uses <my-greeting>. In the demo, this is defined within a <script> tag.

Notice:

  • There is content within <my-greeting>. This is called the light or user content.
  • The content looks for a site value.

Source data:

stache("...")({
    site: "CanJS"
});

This is how we render the source view that uses <my-greeting>. The view is rendered with site in its ViewModel.

HTML Result:

<header>
  <my-greeting>
    <h1>CanJS - can-component</h1>
  </my-greeting>
</header>

This is the result of the view transformations. The user content within the original <my-greeting> is placed within the start of the <h1> tag. Also, notice that the user content is able to access data from the source data.

The following sections break this down more.

View insertion

The view specified by view is rendered directly within the custom tag.

For example the following component:

Component({
  tag: "my-greeting",
  view: stache("<h1>Hello There</h1>")
});

With the following source html:

<header>
  <my-greeting></my-greeting>
</header>

Produces the following html:

<header>
  <my-greeting><h1>Hello There</h1></my-greeting>
</header>

However, if there was existing content within the source html, like:

<header>
  <my-greeting>DO REMOVE ME!!!</my-greeting>
</header>

…that content is removed and replaced by the component’s view:

<header>
  <my-greeting><h1>Hello There</h1></my-greeting>
</header>

The <content> element

Use the <content> element to place the source content in the component’s element within the component’s view. For example, if we change the component to look like:

Component({
  tag: "my-greeting",
  view: stache("<h1><content/></h1>")
});

and rendered with source html, like:

<my-greeting>Hello World</my-greeting>

it produces:

<my-greeting><h1>Hello World</h1></my-greeting>

<content> element default content

If the user does not provide source content, the html between the <content> tags will be used. For example, if we change the component to look like:

Component({
  tag: "my-greeting",
  view: stache("<h1><content>Hello World</content></h1>")
});

and rendered with source html like:

<my-greeting></my-greeting>

it produces:

<my-greeting><h1>Hello World</h1></my-greeting>

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