根据我的Emberjs模型中的条件显示特定值(Display a certain value based on a condition in my Emberjs Model)

编程入门 行业动态 更新时间:2024-10-16 22:12:17
根据我的Emberjs模型中的条件显示特定值(Display a certain value based on a condition in my Emberjs Model)

自从我来自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

更多推荐

本文发布于:2023-08-04 10:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   条件   定值   Emberjs   Model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!