2012-01-11 60 views
0

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

.... 
+0

恩,这里有几件事情,但首先,你的InitView代码在哪里? – JayC 2012-01-11 01:11:06

+0

谢谢JayC!我用视图更新我的帖子! – trouble 2012-01-11 08:55:43

+0

很高兴你知道了。 – JayC 2012-01-12 18:31:55

回答

1
init: function(){ 
    this.initial = new Init();  
    this.initialView = new InitView({model: this.initial}); 
    this.initial.fetch(); 
} 

看起来就像我预料,但这:

clefs = new CustomCollection(); 
clefs.add(response.clefs);   
this.set({clefs: clefs}); 

没有这么多。我自己是一个Backbone.js newb,所以我仍然在学习处理复合模型的最佳方法。我之前所做的是有一个观点跟踪两个模型(因为它显示了一个列表清单,但在任何给定时间只有一个子列表可见)。每当单击第一个列表中的一个项目时,就会选择包含子列表的模型,并从该模型中“获取”该字段,将其转换为JSON()它(这是一个非常不幸的名称,因为它不是JSON字符串,而是对象表示),然后重置绑定到视图的模型以显示当前子列表。没有代码,这有点难以解释,但目前我的时间有点短缺。后来我可能会尝试更好地解释(也许有另一个答案)。

编辑1

只是为了澄清,我反对的是通过set分配和后来通过get检索。我只是不确定Backbone.js如何处理。不过,我很想知道。

EDIT 2

你可能想看看: backbone.js structuring nested views and models

基本上,他建议,而不是通过一系列分配,你只要把复合模型的子模型的属性

所以不是

parse: function(response) { 

    clefs = new CustomCollection(); 
    clefs.add(response.clefs);   
    this.set({clefs: clefs}); 

    ..... 

哟u'd有

parse: function(response) { 

    this.clefs = new CustomCollection(); 
    this.clefs.add(response.clefs); 
    /*Or maybe you really mean this.clefs.reset(response.clefs)*/ 

    ..... /* something similar for rests, etc */ 

} 

,然后如果你想绑定一个特定的视图,在你的路由器,假设你已经定义CleftsView,RestsView等

init: function(){ 
    this.initial = new Init();  
    this.initialView = new InitView({model: this.initial}); 
    this.clefsView = new ClefsView({model: this.initial.clefs}); 
    this.restsView = new RestsView({model: this.initial.rests}); 

    ..... 

    this.initial.fetch(); 
    ..... 

    } 

我可能会认为这更早,但我不确定这是否是普遍的做法。我仍然不确定是否喜欢这个建议,因为我试图将其余模型和模型放在Init的“模型”属性下,而不是直接在Init之下。

+0

很棒的评论JayC和谢谢你!但“this.initial.clefs”总是返回“未定义”真的不明白为什么!无论是“谱号”属性还是属性!! :( – trouble 2012-01-12 00:34:37

1

这两条线没有意义可言:

init: function(){ 
    this.init = new Init(); 
    this.initView = new InitView({collection: this.init.get("rests") }); 
    this.init.fetch(); 

首先你在你的路由器中有一个初始化函数,而调用this.init = new Init();会覆盖这个函数,所以当再次调用这个路由时,init不再使用你的函数,而是你的模型。接下来你创建一个没有任何数据的新模型,所以它是空的。比你试图从空模型中“休息”。之后你填写模型。也许你想是这样的:

init: function(){ 
    //create a new model, fetch the data from the server. On success create a new view passing data from the model. 
    var init = new Init().fetch({ 
     success: function(){new InitView({collection: init.get("rests") });} 
    }); 
+0

谢谢安德烈亚斯!我更新我的代码!我的例子仍然存在错误! – trouble 2012-01-11 08:56:44