自从我来自java服务器端世界以来,我对Emberjs很新。
我面临的实际情况如下。 我有一个会员模特,像这样
import DS from 'ember-data'; export default DS.Model.extend({ firstname : DS.attr('string'), lastname: DS.attr('string'), ... paid_fees : DS.hasMany('paid-fee', {async : true}) });......和付费模式
import DS from 'ember-data'; export default DS.Model.extend({ year : DS.attr('number'), amount: DS.attr('number') });这是我的问题,如何显示成员列表并在其中一列中显示例如“V”,如果其中一个付费费用为2016年。
这是我写的模板:
<table class="table table-hover"> <tr> <th>Prénom</th> <th>Nom</th> <th>Email</th> <th>Gsm</th> <th>En ordre de côtisations</th> <th>Détails</th> </tr> <tbody> {{#each model as |member|}} <tr> <td>{{member.firstname}}</td> <td>{{member.lastname}}</td> <td>{{member.email}}</td> <td>{{member.mobilePhone}}</td> <td> /* How-TO ??? Component ? Computed property ? Helpers ? */ </td> <td>{{#link-to 'members.show' member}}<span class="glyphicon glyphicon-zoom-in" />{{/link-to}}</td> </tr> {{else}} No data available {{/each}} </tbody> </table>有帮助吗? 谢谢奥利维尔
I am very new to Emberjs since I come from java server side world.
The actual case I'm facing to is the following. I got a Member model, like this
import DS from 'ember-data'; export default DS.Model.extend({ firstname : DS.attr('string'), lastname: DS.attr('string'), ... paid_fees : DS.hasMany('paid-fee', {async : true}) });... and the paid-fee Model
import DS from 'ember-data'; export default DS.Model.extend({ year : DS.attr('number'), amount: DS.attr('number') });Here is my problem, how to display a list of member and in one of the column, display for example an 'V' if one of the paid-fees has the year 2016.
Here is the template I wrote:
<table class="table table-hover"> <tr> <th>Prénom</th> <th>Nom</th> <th>Email</th> <th>Gsm</th> <th>En ordre de côtisations</th> <th>Détails</th> </tr> <tbody> {{#each model as |member|}} <tr> <td>{{member.firstname}}</td> <td>{{member.lastname}}</td> <td>{{member.email}}</td> <td>{{member.mobilePhone}}</td> <td> /* How-TO ??? Component ? Computed property ? Helpers ? */ </td> <td>{{#link-to 'members.show' member}}<span class="glyphicon glyphicon-zoom-in" />{{/link-to}}</td> </tr> {{else}} No data available {{/each}} </tbody> </table>Any help ? Thanks Olivier
最满意答案
你正在寻找的是一个PromiseObject,它将让你有一个计算属性,它返回一个代理其值的promise:
hasPaidThisYear: Ember.computed('paid_fees.@each.year', function() { return DS.PromiseObject.create({ promise: this.get('paid_fees').then(function(paidFees) { return paidFees.any(function(paidFee) { return paidFee.get('year') === 2016; }); }) }); })只需将其放入Member模型中,您就可以在模板中使用{{if member.hasPaidThisYear 'V'}} 。
PromiseObject的API文档: http ://emberjs.com/api/data/classes/DS.PromiseObject.html
What you are looking for is a PromiseObject which will let you have a computed property that returns a promise which proxies its value:
hasPaidThisYear: Ember.computed('paid_fees.@each.year', function() { return DS.PromiseObject.create({ promise: this.get('paid_fees').then(function(paidFees) { return paidFees.any(function(paidFee) { return paidFee.get('year') === 2016; }); }) }); })just put this in the Member model, and you will be able to use {{if member.hasPaidThisYear 'V'}} in your template.
API docs for PromiseObject: http://emberjs.com/api/data/classes/DS.PromiseObject.html
更多推荐
发布评论