tag
Register custom behavior for a given tag.
can.view.tag( tagName, tagHandler(el, tagData) ) 2.1
Registers the tagHandler
callback when tagName
is found
in a template. Check out this video where we talk about different possiblities to use can.view.tag:
Parameters
-
tagName
{String}
A lower-case, hypenated or colon-seperated html tag. Example:
"my-widget"
or"my:widget"
. It is considered a best-practice to have a hypen or colon in all custom-tag names. -
tagHandler
{function(el, tagData)}
Adds custom behavior to
el
. IftagHandler
returns data, it is used to rendertagData.subtemplate
and the result is inserted as the childNodes ofel
.
Use
can.view.tag
is a low-level way to add custom behavior to custom elements. Often, you want to do this with can.Component. However,can.view.tag
is useful for when can.Component might be considered overkill. For example, the following creates a jQueryUI DatePicker everytime a<jqui-datepicker>
element is found:The
tagHandler
's tagData argument is an object that contains the mustache scope and helper options whereel
is found and a subtemplate that renders the contents of the template within the custom tag.Getting values from the template
tagData.scope
can be used to read data from the template. For example, if I wanted the value of"format"
within the current template, it could be read like:tagData.options
contains the helpers and partials provided to the template. A helper function might need to be called to get the current value of format like:Responding to changing data
Often, data passed to a template is observable. If you use
can.view.tag
, you must listen and respond to chagnes yourself. Consider if format is property on asettings
can.Map like:You want to update the datepicker if
format
changes. The easist way to do this is to use Scope's compute method which returns a get-set compute that is tied to a key value:If you listen on something outside the tag, it's a good practice to stop listening when the element is removed from the page:
Subtemplate
If content is found within a custom tag like:
A seperate template function is compiled and passed as
tagData.subtemplate
. That subtemplate can be rendered with custom data and options. For example:In this case, the sub-template will not get a value for
last
. To include the original data in the subtemplate's scope, [can.view.Scope::add add] to the old scope like: