Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between $(this.el) and this.$el in Backbone
    primarykey
    data
    text
    <p>I'm developing a single-page web application using Backbone and Laravel. I've set my router to use pushState and configured Laravel to send all other requests to the main view of the backbone application, where backbone takes care of the routing.</p> <p>My problem/question is as follows:</p> <p>I have a route called 'dashboard', this route is the main application view and is shown after login. It uses a collection called Clients.</p> <pre><code>dashboard:function(uri){ dashboardCallback = function(data){ if(data.check){ console.log('generate dashboard'); //get clients collection clientsCollection = new Dash.Collections.Clients(); clientsCollection.fetch().then(function(clients){ //genenerate dashboard view new Dash.Views.Dashboard({collection:clientsCollection}).renderDashboard(); }); } else{ router.navigate('/', {trigger:true, replace:true}); } } Dash.Utilities.user.isLoggedIn(dashboardCallback); }, </code></pre> <p>The <code>Dash.Views.Dashboard</code> view takes care of all the views in the application, when calling the renderDashboard(); method, it starts rendering all client views. This is where it gets interesting. </p> <p>The code for rendering all the client views is as follows:</p> <pre><code>renderClients:function(){ console.log('Rendering all clients', this.collection); clientsView = new Dash.Views.Clients({collection:this.collection}).render(); $(this.el).html(clientsView.el); } </code></pre> <p>with the above code, it works in all cases. With that i mean when I log in first and the application routes me to the dashboard view all the clients gets rendered and appended to the DOM, the same thing happens when I access <code>/dashboard</code> immediately (afther the application checks if i'm logged in). </p> <p>But, when I use the following code it doesn't load the client views when I first log in. It does load the client views when i access <code>/dashboard</code> directly.</p> <pre><code>renderClients:function(){ console.log('Rendering all clients', this.collection); clientsView = new Dash.Views.Clients({collection:this.collection}).render(); this.$el.html(clientsView.el); } </code></pre> <p>It took me a while to figure out that the fix of the problem was that I had to replace <code>this.$el</code> with <code>$(this.el)</code>, but I alway's thought it didn't matter because they are essentially the same, or am I wrong in this assumption?</p> <p>Can someone explain to me this weird behaviour?</p> <p>As requested, here is my global Dashboard view</p> <pre><code>Dash.Views.Dashboard = Backbone.View.extend({ tagName:'div', id:'main', className:'dashboard', initialize: function(){ console.log('Initializing Global Dashboard View'); //make sure the main element is only added once. if(!$('.dashboard').length){ $('body').append(this.el); } else{ this.el = $('.dashboard'); } }, renderDashboard: function(){ console.log('Render all Dashboard components'); this.renderNavBar(); this.renderClients(); }, renderNavBar: function(){ var navBarView = new Dash.Views.NavBar().render(); $(this.el).before(navBarView.el); }, renderLogin: function(){ var logInView = new Dash.Views.Login().render(); $(this.el).html(logInView.el); }, renderWhoops:function(error){ console.log('Render Whoops from Global Dashboard'); var whoopsModel = new Dash.Models.Whoops(error); $(this.el).html(new Dash.Views.Whoops({model:whoopsModel}).render().el) }, renderClients:function(){ console.log('Rendering all clients', this.collection); clientsView = new Dash.Views.Clients({collection:this.collection}).render(); $(this.el).html(clientsView.el); } }); </code></pre>
    singulars
    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.
 

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