Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call jQuery code after Backbone template is rendered?
    primarykey
    data
    text
    <p>I am building an app with Backbone.js (using Underscore templates), Express and Node.js.</p> <p>I made this very simple example to illustrate the problem (see code below), the app consists in a list of "titles" retrieved from the server.</p> <p>I use an underscore template to render the HTML. After the HTML is rendered I would like to execute some jQuery code, like for example print in the console the position() of each "title". <strong>The problem is that the jQuery code gets called before the template has inserted the elements in the DOM, so jQuery can't find the elements.</strong></p> <p>What am I missing? <strong>Is there a way to get an event after the html is inserted in the DOM?</strong></p> <p>Here the code:</p> <pre><code> &lt;div class="container"&gt; &lt;div id="tracks"&gt; &lt;/div&gt; &lt;/div&gt; &lt;!-- Template --&gt; &lt;script id="trackTemplate" type="text/template"&gt; &lt;ul id="&lt;%= id2 %&gt;" style="height:150px"&gt; &lt;li&gt;&lt;%= title %&gt;&lt;/li&gt; &lt;/ul&gt; &lt;% // So it looks like this code gets code before the template's HTML is in the DOM. console.log($("#"+id2).position()); %&gt; &lt;/script&gt; &lt;script src="js/lib/jquery-1.10.2.min.js"&gt;&lt;/script&gt; &lt;script src="js/lib/underscore-min.js"&gt;&lt;/script&gt; &lt;script src="js/lib/backbone-min.js"&gt;&lt;/script&gt; &lt;script&gt; var app = app || {}; // Model app.Track = Backbone.Model.extend({ defaults: { id2: '', title: 'Track', }, }); // Collection app.Library = Backbone.Collection.extend({ model: app.Track, url: '/api/tracks' }); // Track view app.TrackView = Backbone.View.extend({ tagName: 'div', className: 'trackContainer', template: _.template( $( '#trackTemplate' ).html() ), initialize:function(){ this.model.on('change', this.render, this); }, render: function() { this.$el.html( this.template( this.model.toJSON() ) ); return this; }, }); // Collection View var numberOfTracks = 0; app.LibraryView = Backbone.View.extend({ el: '#tracks', initialize: function( initialTracks ) { this.collection = new app.Library( initialTracks ); this.collection.fetch(); this.listenTo( this.collection, 'add', this.renderTrack); }, // render library by rendering each track in its collection render: function() { this.collection.each(function( item ) { this.renderTrack( item ); numberOfTracks++; }, this ); }, // render a track by creating a trackView and appending the // element it renders to the library's element renderTrack: function( item ) { var trackView = new app.TrackView({ model: item }); this.$el.append( trackView.render().el ); }, }); // Initialise app var libraryView = new app.LibraryView(); &lt;/script&gt; </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload