Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : Best way to bind a templated event to object created by Control

$
0
0
A bindEvents does make sense but basically would be the same thing as .on().
What I suggest in your case is still binding to a selector instead of the actual button element:
  1. can.Control({
  2. defaults : {
  3. newButton : 'new-button'
  4. }
  5. }, {
  6. init: function() {
  7.      this.element.after($("<button>Click Me</button>").addClass(this.options.newButton));
  8. }

  9. ".{newButton} click": function() {
  10.      //do something
  11. }
  12. });

What you can also do is add the newButton element during setup() which allows you to avoid calling .on() again during init:

  1. can.Control({
  2. setup: function(el, options) {
  3. var newButton = $("<button>Click Me</button>");
  4. this.element.after(newButton);
  5. return can.Control.prototype.setup.cal(this, el, can.extend({
  6. newButton : newButton
  7. }, options));
  8. }

  9. "{newButton} click": function() {
  10.      //do something
  11. }
  12. });

Viewing all articles
Browse latest Browse all 3491

Trending Articles