2012-08-03 73 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