在哪里和如何包括Ember模型函数 [英] Where and how to include Ember model functions

查看:161
本文介绍了在哪里和如何包括Ember模型函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个简单的Ember应用程序,显示列表元素的ID。 ID是使用基于元素序列的简单函数(和一些其他简单因子)来计算的。例如:

 < div class =content-id element> [ELEMENT ID]< / div> 
< div class =content-name> {{element.name}}< / div>

我不想将元素ID硬编码到JSON对象中,而是将其编码为简单的功能。最好将此函数放在路由对象或组件中吗?什么是语法?



谢谢。

解决方案

要仅基于模型属性计算某些内容,则应使用计算属性。在以下示例中,我定义了一个计算属性 citiesString

 导入DS 余烬数据; 
从ember导入Ember;

export default DS.Model.extend({
name:DS.attr('string'),
cities:DS.hasMany('city',{async:false })

citiesString:Ember.computed('cities',function(){
var cities = [];
this.get('cities')forEach函数(item,index,enumerable){
cities.push(item.get('name'));
},this);

return cities.join ,');
})
});

您可以像其他任何一样使用计算属性:

  {{#each elements as | element index |}} 
{{element.citiesString}}
{{/ each}}

如果要使用元素的索引(如果我理解元素序列对),我不能想到其他方式比写一个帮手,传递给它元素,索引和返回ID。助手示例:

  // app / helpers / my-helper.js 
import ember from'ember';

导出默认值Ember.Handlebars.makeBoundHelper(function(element,index){
return index; //做一些有用的而不是这个
});

如何使用它:

  {{#each elements as | element index |}} 
{{my-helper element index}}
{{/ each}}

I am working on a simple Ember app that shows ID's for list elements. The ID's are computed with simple functions based on the element sequence (and some other simple factors). For example:

<div class="content-id element">[ELEMENT ID]</div>
<div class="content-name">{{element.name}}</div>

I don't want to hardcode the element id's into the JSON object, but rather compute them in a simple function. Would it be best to place this function in the route object or in the component? What's the syntax?

Thank you.

解决方案

If you want to compute something based only on model properties, you should use a computed property. In following example I define a computed property citiesString:

import DS from 'ember-data';
import Ember from 'ember';

export default DS.Model.extend({
  name: DS.attr('string'),
  cities: DS.hasMany('city', { async: false }),

  citiesString: Ember.computed('cities', function () {
    var cities = [];
    this.get('cities').forEach(function (item, index, enumerable) {
      cities.push(item.get('name'));
    }, this);

    return cities.join(', ');
  })
});

You can use computed property like any other:

{{#each elements as |element index|}}
  {{element.citiesString}}
{{/each}}

If you want to use element's index (if I understand "element sequence" right), I can't think out other way than writing a helper, pass to it element, index, and return ID. Example of helper:

//app/helpers/my-helper.js
import Ember from 'ember';

export default Ember.Handlebars.makeBoundHelper(function (element, index) {
  return index; //do something useful instead of this
});

How to use it:

{{#each elements as |element index|}}
  {{my-helper element index}}
{{/each}}

You should keep in mind that index can change (as can order of element in array) and different users may see different ID for the same element.

这篇关于在哪里和如何包括Ember模型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆