Wednesday, June 4, 2014

Accessing multiple objects from CanJS events in components

CanJS components have nice and easy way to access context object from event, just as parameter:

  <input type="button" can-click="add" />

add: function(book) {

But what if you need also parent object, or object from some other hierarchy. To do it, CanJS can bind DOM objects to any data, and in components this is usually context. Just add special property {{data '...'}} to DOM element in your Mustache template, for example:

  <div {{data 'shelf'}}>
      <input type="button" can-click="add" {{data 'book'}} />

And you can access all objects like:

add: function(context, el) {
  var book ='book');
  var shelf = el.parent().data('shelf');

No comments:

Post a Comment