Thursday, 15 July 2010

javascript - Backbone.js events on templates -


I am trying to allocate an event to a button by using backbone events:. I tried to do all this, but nothing has been working till now. My suspicion is that I can not add events because $ el's content has not been updated yet (I am using underscores in a template). So my question will be how can I assign events to those classes that are not initially loaded?

My code is on Githubo in the repo

The file I am working on is located under the UI / Script / view / UserPage.js.

  Var UserView = Backbone.View.extend ({initialize: function () {}, render: function () {var data = {}; var compiled = _.template (UserPage, data ); $ When ($ ('.main') .html (compiled)). (Function () {this. $ El = $ (". UserEdit"); console.log (this. $ El);}) ;}, Event: {"click .example"}: "OpenPreview"}, OpenPreview: Function () {Warning ("HI");}   

This template ui / script / View / Template is located in the user page.

  
div class = "header"> gt; & lt; h2 & gt; user Edit & lt; / h2 & gt; & lt; button & gt; & l T: / button>

PS: I have already tried to solve this problem but I failed

Your event after you changed this. $ El Is not being given the correct element. $ El , this will work with the delegation again for you.

In addition, you will need to create a view variable to keep the context of this , because inside callback No reference now.

Your render function should look like this:

  render: function () {var data = {}; Var compiled = _template (UserPage, data); Var Views = This; $ .when ($ ('main') .html (compiled)). (Function () {view.setElement ($ (". UserEdit")); console.log (see. $ El;)}); }    

No comments:

Post a Comment