2012-01-11 90 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