Note that there are some explanatory texts on larger screens.

plurals
  1. POchild collections aren't rendering in parent model view in backbone
    primarykey
    data
    text
    <p>I can't seem to get the children view templates to render. They are showing up in console log, and they are showing up the right amount of times, but I can't get them to render in the browser.</p> <p>The goals are the parents and the steps are the children. </p> <p>CODE BITS:</p> <p>models:</p> <pre><code>$(function() { window.Goal = Backbone.Model.extend({ defaults: { description: null }, initialize: function() { this.steps = new Steps(); this.steps.fetch({ reset: true }); this.stepsAll = new StepsViewForGoals({ collection:this.steps }); $('.all-steps').append(this.stepsAll.render().el); } }); window.Goals = Backbone.Collection.extend({ model: Goal, url: '/api/goals/' }); window.goals = new Goals(); }); </code></pre> <p>goals views:</p> <pre><code>$(function() { window.GoalView = Backbone.View.extend({ className: 'list-item', template: _.template($('#goal-item-template').html()), initialize: function() { _.bindAll(this, 'render'); this.model.bind('change', this.render); }, render: function() { this.$el.html(this.template(this.model.toJSON())); return this; } }); window.GoalsView = Backbone.View.extend({ el: '#app', template: _.template($('#goals-list-template').html()), initialize: function () { _.bindAll(this, 'render', 'addGoal'); this.collection.bind('reset', this.render); this.collection.bind('change', this.render); this.collection.bind('add', this.render); }, render: function() { this.$el.html(this.template()); this.collection.each(function(goal) { var view = new GoalView({ model:goal }); $('#goals').append(view.render().el); }); return this; } }); }); </code></pre> <p>step views</p> <pre><code>$(function() { window.StepView = Backbone.View.extend({ className: 'list-item', template: _.template($('#step-item-template').html()), initialize: function () { _.bindAll(this, 'render'); this.model.bind('change', this.render); }, render: function() { this.$el.html(this.template(this.model.toJSON())); console.log('Individual step'); return this; } }); window.StepsViewForGoals = Backbone.View.extend({ el: '.all-steps', template: _.template($('#step-list-template').html()), initialize: function () { _.bindAll(this, 'render'); this.collection.bind('reset', this.render); this.collection.bind('change', this.render); }, render: function() { this.$el.html(this.template()); console.log(this.collection.toJSON()); this.collection.each(function(step) { var view = new StepView({ model:step }); $('.all-steps-list').append(view.render().el); }); console.log('Steps View'); return this; } }); }); </code></pre> <p>Parent model template:</p> <pre><code>{% verbatim %} &lt;script id="goal-item-template" type="text/template"&gt; &lt;h4&gt;&lt;a class="controls-toggle"&gt;&lt;%- description %&gt;&lt;/a&gt;&lt;/h4&gt; &lt;div class="controls"&gt; &lt;a class="edit"&gt; &lt;span class="ss-icon ss-pika edit-toggle"&gt;edit&lt;/span&gt; &lt;span class="ss-icon ss-pike save-toggle"&gt;save&lt;/span&gt; &lt;/a&gt; &lt;a class="remove"&gt; &lt;span class="ss-icon ss-pika"&gt;delete&lt;/span&gt; &lt;/a&gt; &lt;/div&gt; &lt;div class="edit-func"&gt; &lt;div class="form-block"&gt; &lt;textarea name="description"&gt;&lt;%- description %&gt;&lt;/textarea&gt; &lt;/div&gt; &lt;div class="all-steps"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/script&gt; {% endverbatim %} </code></pre> <p>child list template</p> <pre><code>{% verbatim %} &lt;script id="step-list-template" type="text/template"&gt; &lt;h1 class="section-title"&gt;My Steps&lt;/h1&gt; &lt;div id="steps" class="all-steps-list"&gt;&lt;/div&gt; &lt;/script&gt; {% endverbatim %} </code></pre> <p>Router for Clarity:</p> <pre><code>$(function() { window.AppRouter = Backbone.Router.extend({ routes: { 'goals/': 'goals', 'steps/': 'steps' }, initialize: function() { // Goals this.goalsview = new GoalsView({ collection: goals }); goals.fetch({ reset:true }); this.stepsview = new StepsView({ collection: steps }); steps.fetch({ reset:true }); }, goals: function () { $('#app').empty(); $('#app').append(this.goalsview.render().el); }, steps: function () { $('#app').empty(); $('#app').append(this.stepsview.render().el); } }); window.appRouter = new AppRouter(); Backbone.history.start(); }); </code></pre>
    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