javascript - Nested Backbone Models/Looping on Arrays in CoffeeScript -


i'm working on creating basic survey rails app learn nested models. i'm using backbone-relational relate models.

i think have them related correctly, i'm struggling displaying each of questions in survey. inside survey template can @survey.get('questions').length , correct, reason .each , for question in @survey.get('questions') both throw error.

i tried route of looping through questions in view itself, throws error

any me getting work appreciated!

survey_show.jst.eco template

<h2><%= @survey.get('title') %></h2> <div class="row">     <div class="col-md-8"         <span class="pull-right">number taken: <%= @survey.get('number_taken') %>/<%= @survey.get('survey_limit') %></span>     </div> </div> <ul class="list-group" id="questions"> <% @survey.get("questions").each %> <li class="list=group-item">derp</li> <% end %> </ul> 

survey.js.coffee survey model

class surveyme.models.survey extends backbone.relationalmodel    defaults:      title: 'enter survey name'     survey_limit: 1000     number_taken: 0     long: false     survey_finished: false     urlroot: '/surveys'     relations = [{      type: backbone.hasmany,      key: 'questions',      relatedmodel: 'surveyme.models.question'      reverserelation: {         key: 'survey',         includeinjson: 'id'      }    }] 

question.js.coffee question model

class surveyme.models.question extends backbone.relationalmodel     urlroot: '/surveys'     relations = [{      type: backbone.hasmany,      key: 'answers',      relatedmodel: 'surveyme.models.answer'      reverserelation: {         key: 'question',         includeinjson: 'id'      }    }] 

survey_show.js.coffee survey show view

  class surveyme.views.surveyshow extends backbone.view    template: jst['templates/surveys/survey_show']    initialize: ->     @model.on('all', @render, this)     @model.on('change', @alert, this)    events: {     'click #back': 'back'   }    render: ->     $(@el).html(@template(survey: @model))     #@model.questions.each(@addquestion)        back: ->     backbone.history.navigate("surveys",true)    alert: ->     alert('changed titled')    addquestion: (question) ->     view = new surveyme.views.question(model: question)     @$('#questions').append(view.render().el) 

question view

class surveyme.views.question extends backbone.view    template: jst['templates/surveys/question']    tagname: 'li'    classname: 'list-group-item'    events:      'click': 'edit'    edit: ->     backbone.history.navigate("questions/#{@model.get('id')}",true)    initialize: ->     @render    render: ->     $(@el).html(@template(question: @model))     

question template

<%= @question.get('title') %> <span class="pull-right"><%= @question.get('answers').length %></span> 

i figured out. missing colon @ end. got me wanted:

<ul class="list-group"> <% question, in @survey.get("questions"): %>     <li class="list-group-item">         <%= @survey.get("questions")[i]["title"] %>         <ul class="list-group">             <% answer, j in @survey.get("questions")[i]["answers"]: %>             <li class="list-group-item">                 <%= @survey.get("questions")[i]["answers"][j]["title"] %>             </li>             <% end %>         </ul>     </li> <% end %> </ul> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -