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

viewModel

  • Edit on GitHub

Return the view model instance or type with which the component’s view is rendered. This is used when more fine grained control is needed over ViewModel.

function(properties, parentScope, element)

The viewModel function takes the properties and values that are used to typically initialize a ViewModel, the can-view-scope the component is rendered within, and the component’s element and returns either the view-model instance or ViewModel type that the component’s view is rendered with.

This is typically used only for special situations where a custom scope or custom bindings need to be setup.

var Component = require("can-component");
var Scope = require("can-view-scope");

Component.extend({
    tag: "my-element",
    viewModel: function(properties, scope, element){
        var vm =  new DefineMap(properties);
        // do special stuff ...
        return vm;
    }
});

stache("<my-element first:from='firstName' last='Meyer'/>")({
  firstName: "Justin",
  middleName: "Barry"
});

Parameters

  1. properties {Object}:

    An object of values specified by the custom element’s attributes. For example, a view rendered like:

    stache("<my-element title:from='name'></my-element>")({
      name: "Justin"
    });
    

    Creates an instance of following control:

    Component.extend({
        tag: "my-element",
        viewModel: function(properties){
          properties.title //-> "Justin";
        }
    });
    

    And calls the viewModel function with properties like {title: "Justin"}.

  2. parentScope {can-view-scope}:

    The viewModel the custom tag was found within. By default, any attribute’s values will be looked up within the current viewModel, but if you want to add values without needing the user to provide an attribute, you can set this up here. For example:

    Component.extend({
        tag: "my-element",
        viewModel: function(properties, parentScope){
          parentScope.get('middleName') //-> "Barry"
        }
    });
    

    Notice how the middleName value is looked up in my-element’s parent scope.

  3. element {HTMLElement}:

    The element the can-component is going to be placed on. If you want to add custom attribute handling, you can do that here. For example:

    Component.extend({
        tag: "my-element",
        viewModel: function(properties, parentScope, el){
            var vm = new DefineMap({clicks: 0});
            domEvent.addEventListener.call(el, "click", function(){
                vm.clicks++;
            });
            return vm;
        }
    });
    

    This example should be done with the events object instead.

Returns

{Map|Object}:

Returns one of the following.

  • An observable map or list type.
  • The prototype of an observable map or list type that will be used to render the component’s view.

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