I have a JSON File looking like this:
JSON :
{
"clefs": [
{"title": "..", "path": ".."},
... ,
{"title": "..", "path": ".."}
],
....
"rests": [
{"title": "..", "path": ".."},
... ,
{"title": "..", "path": ".."}
]
}
This is a nested JSON, right? So I try to convert that into Model/Collections nested into Backbone.js
like this:
Backbone.js :
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});
}
});
Now I came out with a Model and into its Attributes my Collection: clefs, ..., rests.
When it's come to pass my Collection to a View I cannot!
I made this
Router :
$(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();
});
View :
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");
}
});
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 no success!
What i want is to extract my Collections out from the Model name's "initial"!! this.initial.attributes
return an Object with my collections into it! but i cannot pass those to the View!
Update 1 :
Now the Model is passed to the View but i always cannot access his attributes with get or send his attributes to other Views! the render can not be fired too!!!
Update 2 : After several Days of headaches i take the resolution to make it simple!
Why? cause i just have 4 Collections for now : clefs, accidentals, notes and rests
MODEL :
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
ROUTER :
....
this.clefsColl = new ClefsCollection();
this.clefsColl.fetch();
this.clefsCollView = new ClefsView({collection: this.clefsColl});
....