2012-08-03 25 views
4

我有一個模型'事務',其中聲明瞭一個subCategories數組。只要調用transactionsController的方法'add_subcagtegory',該數組就會填充事務類型對象。現在,當我嘗試在嵌套循環(#collection)中呈現子類別時,我沒有完成它。渲染數組控制器對象的外部循環(#each)工作正常。任何人都可以告訴如何呈現subCategories數組?Ember.js /呈現在把手上的嵌套數組內容

app.js

App.transaction=Em.Object.extend({ 
    account:null, 
    date:null, 
    source:null, 
    description:null, 
    category:null, 
    flag_for_later:null, 
    amount:null, 
    category_id:null, 
    record_index:null, 
    isSubCategory:null, 
    subCategories:[] 
}); 

App.transactionsController = Em.ArrayController.create({ 
    content: [], 
    add_subcategory: function(param){ 
    var records=this.toArray(); 
    if (typeof(records[param.value -1].subCategories) === "undefined") { 
     records[param.value -1].subCategories = new Array(); 
    } 


     var category=App.transaction.create({ 
     account:"//", 
     date:"//", 
     source:"//", 
     description:"//", 
     category:" ", 
     flag_for_later:" ", 
     amount:null, 
     category_id:records[param.value -1].subCategories.length + 1, 
     isSubCategory:true 
     }); 

    records[param.value -1].subCategories.push(category); 

    App.transactionsController.set('content',[]); 
    App.transactionsController.pushObjects(records); 

    App.array.push(obj1); 
    } 
}); 

和模板:

<table> 
    {{#each App.transactionsController}} 
     <tr> 
     <td>{{account}}</td> 
     <td>{{date}}</td> 
     <td>{{source}}</td> 
     <td>{{view App.TextField class="span12" style="border:0px;" objcount=record_index fieldname="description" value=description}}</td> 
     <td>{{view App.TextField class="span12" style="border:0px;" objcount=record_index fieldname="category" value=category }}</td> 
     <td><button onclick="App.transactionsController.add_subcategory(this);" value="{{unbound record_index}}">+</button></td> 
     <td>{{view App.TextField class="span6" style="border:0px;" objcount=record_index fieldname="flag_for_later" value=flag_for_later }}</td> 
     <td>{{amount}}</td> 
     </tr> 
     {{#collection contentBinding="App.transactionsController.subCategories"}} 
     <b>content does,nt not render</b> 
     {{/collection}} 
    {{/each}} 
</table> 
下收集模板

,如何訪問子類別?

http://jsfiddle.net/KbN47/29/

回答

2

是否簡單綁定{{收集}}幫手來this.subcategories內容(this是您的上下文事務)工作?

{{#collection contentBinding="this.subcategories"}} 

更新

這裏是一個的jsfiddle:http://jsfiddle.net/Sly7/tRbZC/

請注意餘燼版本是最新一。你應該更新,因爲0.9.5已經很老了。 我沒有看到<select>的行爲,但如果它不起作用,我認爲你現在已經擁有所有的鍵了:)

+0

沒有錯誤,但仍然沒有,NT工作 – Zed 2012-08-03 09:49:13

+0

這很奇怪,因爲你定義子類別作爲交易對象的數組...你可以發佈您的代碼的的jsfiddle? – 2012-08-03 09:50:57

+0

好吧,看起來像add_category功能是越野車。我今天下午去看看。你可以用jsfidlle編輯你的問題,或者至少說明你如何使用這個函數? – 2012-08-03 10:02:05