node package manager

can-view-model

can-view-model

Build Status

Gets or sets the view model of an element

API

can-view-model function

Gets the ViewModel of an element.

canViewModel(element)

Gets the map instance associated with element, creating one as a [can-types.DefaultMap] if it doesn’t already exist, and returns the map.

var vm = canViewModel(element);
  1. element {HTMLElement}: Any element in the DOM.
  • returns {can-map|can-define/map/map|Object}: The ViewModel associated with this element.

canViewModel(element, property)

Gets the map instance associated with element, creating one as a [can-types.DefaultMap] if it doesn’t already exist. Then gets the property inside of the ViewModel and returns that.

var foo = canViewModel(element, "foo");

console.log(foo); // -> "bar"
  1. element {HTMLElement}: Any element in the DOM.
  2. property {String}: The property to get from the ViewModel.
  • returns {*}: The value of the property on the ViewModel or undefined if the property doesn’t exist.

canViewModel(element, property, value)

Gets the map instance associated with element, creating one as a [can-types.DefaultMap] if it doesn’t already exist. Sets the property on that map to value.

canViewModel(element, "foo", "bar");
 
var foo = canViewModel(element, "foo");
 
console.log(foo); // -> "bar" 
  1. element {HTMLElement}: Any element in the DOM.
  2. property {String}: The property that is being set on the ViewModel.
  3. value {*}: The value being set on the property.
  • returns {HTMLElement}: The element.

Contributing

Making a Build

To make a build of the distributable files into dist/ in the cloned repository run

npm install
node build

Running the tests

Tests can run in the browser by opening a web server and visiting the test.html page. Automated tests that run the tests from the command line in Firefox can be run with

npm test