Note that there are some explanatory texts on larger screens.

plurals
  1. POBootstrap mongoose models into BackboneJS
    text
    copied!<p>I've got the following embedded docs Mongoose model defined</p> <pre><code> var mongoose = require("mongoose"); module.exports = function(mongoose) { var Schema = mongoose.Schema, chapters, articles; Articles = new Schema({ identity: Number, order: Number, quick_title: String, full_title: String, last_edited: Number, contributor: [String], content: String, video_link: String, presentation_link: String, question_id: [Number], show_in_chapter: Boolean, summary_text: String, summary_image: String }); chapters = new Schema({ order: Number, title: String, articles: [Articles] }); return mongoose.model('chapters', chapters); } </code></pre> <p>After getting the entire model database through a .find, I'm currently attempting to bootstrap it into a backboneJS model by using the author recommended method (using EJS templating engine):</p> <pre><code> var Chapters = Backbone.Collection.extend({}); chapters = new Chapters( JSON.stringify(&lt;%=chapters%&gt;)); </code></pre> <p>I've tried quite a few ways to get it to work, using the above code it will give me the error Unexpected SyntaxError Unexpected Number (in reference to the first title being Chapter 1)</p> <p>Any ideas on how to turn this Mongoose model into a Backbonejs model? Thanks!</p> <p>UPDATE:</p> <p>Gotten it to work by using stringify before sending it to the template and then within the template, covering it with parentheses: <code>chapters = new Chapters("(" + &lt;%-chapters2%&gt; + ")");</code></p> <p>Unfortunately, this does not give me the Backbone collection I want. When I 'console.log(chapters.toJSON());', it just gives me the following </p> <pre><code>[ Object ([object Object],[object Object],[object Object],[object Object]): Object __proto__: Object ] </code></pre> <p>When using the .at method, there's also only one model at (0). Any ideas?</p>
 

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