訪問屬性的問題我有一個JSON文件看起來像這樣:從嵌套Backbone.js的型號
JSON:
{
"clefs": [
{"title": "..", "path": ".."},
... ,
{"title": "..", "path": ".."}
],
....
"rests": [
{"title": "..", "path": ".."},
... ,
{"title": "..", "path": ".."}
]
}
這是一個嵌套的JSON,對不對?所以,我試圖將其轉換成嵌套到Backbone.js
喜歡這個型號/類別:
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});
}
});
現在,我想出了一個模型,並進入其屬性我的收藏:譜號。休息。
當它來通過我的集合到一個視圖我不能!
我做了這個
路由器:
$(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();
});
查看:
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");
}
});
這是一個醜陋的情況,現在!我有一個具有屬性(集合)的骨幹模型,但我無法提取這些屬性,我嘗試使用JSON(),get,_.each,_.map但沒有成功!
我想要的是從模型名稱的「初始」中提取我的收藏品! this.initial.attributes
返回一個帶有我的集合的對象!但我無法將它們傳遞給視圖!
更新1:
現在模式傳遞給視圖,但我總不能訪問與他的屬性得到或送他的屬性,其他瀏覽!渲染也不能被解僱!!!
更新2: 經過幾天的頭痛後,我採取了使其簡單的決議!
爲什麼?因爲我只是有4個類別現在:譜號,記號,筆記和休息
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
路由器:
....
this.clefsColl = new ClefsCollection();
this.clefsColl.fetch();
this.clefsCollView = new ClefsView({collection: this.clefsColl});
....
恩,這裏有幾件事情,但首先,你的InitView代碼在哪裏? – JayC 2012-01-11 01:11:06
謝謝JayC!我用視圖更新我的帖子! – trouble 2012-01-11 08:55:43
很高興你知道了。 – JayC 2012-01-12 18:31:55