访问属性的问题我有一个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