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

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

$
0
0
Currently I have Control where I create some elements and add them to the DOM via the Control's init() method. They are not contained within the Control's element, so I don't believe normal scoped event handlers will work.
  1. init: function() {
  2.       this.options.newButton = $("<button>Click Me</button>");
  3.       //append the new button to the dom after the control's element
  4.       this.element.after(this.options.newButton);
  5.       //call on to rebind events given this new option
  6.       this.on();
  7. }

  8. "{newButton} click": function() {
  9.       //do something
  10. }

Notice how I have to call on() to rebind the events based on the newButton object I added to this.options.  Otherwise the templated click handler won't do anything.

Not a huge deal, but I am wondering if there is a better/preferred way for me to do this.  Also are there any obvious issues with the approach I am currently taking? 

Thank you,
Keith

Viewing all articles
Browse latest Browse all 3491

Trending Articles