can.viewModel

  • function
 

Read and write a component element's viewModel.

can.viewModel(el[, attr[, value]])

Parameters

  1. el {HTMLElement | NodeList}

    can.Component element to get viewModel of.

  2. attr {String}Optional

    Attribute name to access.

  3. val {*}Optional

    Value to write to the viewModel attribute.

Returns

{*}

If only one argument, returns the viewModel itself. If two arguments are given, returns the attribute value. If three arguments are given, returns the element itself after assigning the value (for chaining).

can.viewModel can be used to directly access a can.Component's viewModel. Depending on how it's called, it can be used to get the entire viewModel object, read a specific property from it, or write a property. The property read and write features can be seen as a shorthand for code such as $("my-thing").viewModel().attr("foo", val);

If using jQuery, this function is accessible as a jQuery plugin, with one fewer argument to the call. For example, $("my-element").viewModel("name", "Whatnot");