viewModel
{Object | can.Map | function(attrs, parentviewModel, element)}
Provides or describes a can.Map constructor function or can.Map instance that will be
used to retrieve values found in the component's template. The map
instance is initialized with values specified by the component element's attributes.
Note: In 2.1, can.stache and can.mustache pass values to the
viewModel differently. To pass data from the viewModel, you must wrap your attribute
value with {}. In 3.0, can.mustache
will use can.stache's syntax.
Use
can.Component's viewModel property is used to define an object, typically an instance of a can.Map, that will be used to render the component's template. This is most easily understood with an example. The following component shows the current page number based off a
limitandoffsetvalue:If this component HTML was inserted into the page like:
It would result in:
This is because the provided viewModel object is used to extend a can.Map like:
Any primitives found on a
can.Map's prototype (ex:offset: 0) are used as default values.Next, a new instance of CustomMap is created with the attribute data within
<my-paginate>(in this case there is none) like:And finally, that data is added to the
parentviewModelof the component, used to render the component's template, and inserted into the element:Values passed from attributes
Values can be "passed" into the viewModel of a component, similar to passing arguments into a function. By default, custom tag attributes (other than
classandid) are looked up in the parent viewModel and set as observable values on the can.Map instance.Values passed in this way are passed similar to function arguments that are "pass by reference" because they are crossbound to the property in the parent viewModel. Changes in the parent viewModel property that is passed in trigger changes in this component's viewModel property, and likewise, changes in the component's viewModel property trigger a change in the parent viewModel property.
As mentioned in the deprecation warning above, using can.stache, values are passed into components like this:
But using can.mustache, values are passed like this:
The rest of the examples in this section show
can.mustachesyntax (which will change to match can.stache in 3.0).Note that using double brackets would instead render the string version of the value, and pass by value rather than by reference:
The above would create an offset and limit property on the component that are initialized to whatever index and size are, NOT cross-bind the offset and limit properties to the index and size.
The following component requires an
offsetandlimit:If
<my-paginate>'s source html is rendered like:...
pageInfo's index and size are set as the component's offset and limit attributes. If we were to change the value ofpageInfo's index like:... the component's offset value will change and its template will update to:
Using attribute values
You can also pass a literal string value of the attribute instead of the attribute's value looked up in the parent viewModel (similar to pass by value).
To do this in can.stache, simply pass any value not wrapped in single brackets, and the viewModel property will be initialized to this string value:
The above will create a title property in the component's viewModel, which has a string
hello. If instead hello was wrapped with brackets like{hello}, a hello property would be looked up in the parent's viewModel.To do this in can.mustache, pass a value like in
can.stache, and set the corresponding viewModel property in the component to@. For example:With source HTML like:
Results in:
In 3.0,
can.mustachesyntax (requiring"@") will change tocan.stache's (not requiring"@").If the tag's
titleattribute is changed, it updates the viewModel property automatically. This can be seen in the following example:Clicking the Change title button sets a
<panel>element'stitleattribute like:Calling methods on viewModel from events within the template
Using html attributes like
can-EVENT-METHOD, you can directly call a viewModel method from a template. For example, we can make<my-paginate>elements include a next button that calls the viewModel'snextmethod like:viewModel methods get called back with the current context, the element that you are listening to and the event that triggered the callback.