Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are misusing the <code>el</code> attribute. <code>$('#content').html(this.view.el)</code> will result in copying the <code>$('#content')</code> element inside itself (because view.el is equal to <code>$('#content')</code>).</p> <p>You should try removing the <code>el</code> attribute from the view object and let it generate itself. Then <code>$('#content').html(this.view.el);</code> should work.</p> <p>One other possible problem is that you are rendering the entire collection inside a <code>li</code> element - was this what you are going for? The best way to go about this would be to have each model in the collection represent a <code>li</code> tag and the collection a <code>ul</code> tag.</p> <p>Other issues:</p> <ul> <li>the <code>view</code> element is receiving a collection but you are trying to render a model</li> <li>in the router, <code>newCollection</code> is not accessible in the home method</li> <li>You are not calling <code>Backbone.history.start()</code></li> </ul> <p>Here is how i would rewrite the code:</p> <pre><code>var model = Backbone.Model.extend({ idAttribute: 'custId' }); var model_view = Backbone.View.extend({ template: Handlebars.compile($("#modelTemplate").html()), tagName: 'li', initialize: function() { _.bindAll(this, 'render'); this.render(); this.on('change',this.render); }, render: function() { $(this.el).html( this.template(this.model.toJSON()) ); return this; } }); var collection = Backbone.Collection.extend({ initialize: function(){ }, model: model, url: '/cust' }); var collection_view = Backbone.View.extend({ tagName: "ul", initialize: function(){ _.bindAll(this, 'render','renderModels'); this.render(); this.renderModels(); this.collection.bind("reset", this.render); this.collection.bind("reset", this.renderModels); }, render: function(){ // just create the 'ul' tag; we will populate it with model view elements; a collection template is no longer needed return this; }, renderModels: function() { this.collection.each(function(obj){ var view = new model_view({ model: obj }); $(this.el).append(view.el); },this); } }); var router = Backbone.Router.extend({ initialize: function(){ this.newCollection = new collection(); this.newCollection.fetch(); }, route: { "": "home" }, home: function(){ this.view = new collection_view({collection: this.newCollection}); $('#content').html(this.view.el); // #content should not be a 'ul' tag, the 'ul' is generated by the collection_view } }); var app = new router(); Backbone.history.start(); </code></pre> <p>Make sure you update your templates accordingly.</p> <p>Please excuse possible errors, i had no means to test the code but i believe it points out the logic you should use.</p> <p>Cheers!</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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