Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with Accessing Attributes from a nested Backbone.js Model
    primarykey
    data
    text
    <p>I have a JSON File looking like this:</p> <p><strong>JSON :</strong></p> <pre><code>{ "clefs": [ {"title": "..", "path": ".."}, ... , {"title": "..", "path": ".."} ], .... "rests": [ {"title": "..", "path": ".."}, ... , {"title": "..", "path": ".."} ] } </code></pre> <p>This is a nested JSON, right? So I try to convert that into Model/Collections nested into <code>Backbone.js</code> like this:</p> <p><strong>Backbone.js :</strong></p> <pre><code>window.initModel = Backbone.Model.extend({ defaults: { "title": "", "path": "" } }); window.CustomCollection = Backbone.Collection.extend({ model: initModel }); window.Init = Backbone.Model.extend({ url : function(){ return "/api/data.json" }, parse: function(response) { clefs = new CustomCollection(); clefs.add(response.clefs); this.set({clefs: clefs}); ..... rests = new CustomCollection(); rests.add(response.rests); this.set({rests: rests}); } }); </code></pre> <p>Now I came out with a Model and into its Attributes my Collection: clefs, ..., rests.</p> <p>When it's come to pass my Collection to a View I cannot!</p> <p>I made this</p> <p><strong>Router :</strong></p> <pre><code>$(document).ready(function() { var AppRouter = Backbone.Router.extend({ routes: { "" : "init" }, init: function(){ this.initial = new Init(); this.initialView = new InitView({model: this.initial}); this.initial.fetch(); } }); var app = new AppRouter(); Backbone.history.start(); }); </code></pre> <p><strong>View :</strong></p> <pre><code>window.InitView = Backbone.View.extend({ initialize : function() { this.model.bind("reset", this.render, this);//this.model.attributes send my 4 Collections Models back! But impossible to extract with "get" these Attributes }, render : function() { console.log("render"); } }); </code></pre> <p>It's an ugly situation right now!! I have a Backbone Model with Attributes(Collections) but I can't extract these Attributes, I try with JSON(), get, _.each, _.map but <strong>no success</strong>!</p> <p><strong>What i want is to extract my Collections out from the Model name's "initial"!! <code>this.initial.attributes</code> return an Object with my collections into it! but i cannot pass those to the View!</strong></p> <p><strong>Update 1 :</strong></p> <p>Now the <strong>Model</strong> is passed to the View but i always cannot access his attributes with <strong>get</strong> or send his attributes to other Views! the render can not be fired too!!!</p> <p><strong>Update 2 :</strong> After several Days of headaches i take the resolution to make it simple!</p> <p>Why? cause i just have 4 Collections for now : clefs, accidentals, notes and rests </p> <p><strong>MODEL :</strong></p> <pre><code>window.initModel = Backbone.Model.extend({ defaults: { "title": "", "path": "" } }); window.ClefsCollection = Backbone.Collection.extend({ model: initModel, url: "/api/data.json", parse: function(response){ return response.clefs; } }); ... and so on </code></pre> <p><strong>ROUTER :</strong></p> <pre><code>.... this.clefsColl = new ClefsCollection(); this.clefsColl.fetch(); this.clefsCollView = new ClefsView({collection: this.clefsColl}); .... </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.
 

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