Ember JS从模型中过滤数据

时间:2018-08-18 14:23:43

标签: ember.js filter

我正在尝试从ember模型中过滤数据,但是找不到解决方案。

文件位于app / routes / notes.js

import Route from '@ember/routing/route';

export default Route.extend({
  model()  {
    let l = this.store.findAll('note');
    return l;
  }
});

文件为app / templates / note.hbs

 {{#each model  as |note|}}
   Title: {{note.title}}<br/>
   Description: {{note.description}}<br/>
   Date:{{note.date}}<br/>
 {{/each}}

我正在尝试过滤模型返回的数据,但是我认为它无法正常工作。在此使用的JSON格式是

{title: title, description: description, date:date, status: status}

我想根据状态过滤输出并显示在模板上。但是我无法修改显示一些错误的数据。我已经尝试从模型本身或通过控制器操作过滤掉,但是没有用。有人可以建议解决方案吗?

1 个答案:

答案 0 :(得分:0)

仅过滤一次:

model() {
  return this.store.findAll('note').then((data) => {
    return data.filter(({ status }) => status === 'HELLO');
  }); // see ES6 Promise specification for .then
}

动态服务器端过滤:

// route.js
queryParameters: {
  status: { refreshModel: true }
},
model({ status }) {
  return this.store.query('note', { status: status });
}

// controller.js
queryParameters: ['status']

status: 'HELLO'

// e.g. user could change 'status' using dropdown

动态客户端过滤:

// route.js
model() {
  return this.store.findAll('note');
}

// controller.js
selectedStatus: 'SOME',
filteredModel: computed('model.length', 'selectedStatus', function() {
  return this.get('model').filterBy('status', this.get('selectedStatus'));
})
相关问题