【问题标题】:Returning 1 object/record from my model in Ember在 Ember 中从我的模型返回 1 个对象/记录
【发布时间】:2015-10-17 00:08:00
【问题描述】:

在我运行的测试应用程序中,我有员工姓名、部门等的固定数据。目前,我的模型包含所有员工,我可以制作员工列表。但是,我想制作一个侧面组件,列出 1 个特定员工的信息(即单击的那个,作为工具提示)。如何仅将 1 名员工传递给我的组件?

我有一个动作,只要有人点击一个名字就更新一个属性。我想做一个计算属性来根据该名称查询模型,但我不确定如何过滤我的模型以仅返回一名员工。

actions: {
  updateProfile(person) {
    set(this, 'profile', person);
  }
}

还有我的计算属性:

currentProfile: computed('profile', function(){
  return this.model.find('person', {'name': get(this, 'profile')});
}),

有没有一种简单的方法可以从我的模型中只返回我想要的 1 个对象?

解决方案

我需要过滤 this 而不是模型本身。然后,我不得不只返回第一个对象,即使只有 1 个匹配项(这是主要问题)。

 currentProfile: computed('profile', function(){
    return this.filterBy('name', get(this, 'profile')).get('firstObject');
 }),

【问题讨论】:

    标签: ember.js ember-data ember-cli


    【解决方案1】:

    试试这个:

    currentProfile: computed('profile', function(){
      var query = {'name': get(this, 'profile')};
      return this.model.find('person', query).then(function(people){
        return people.get('firstObject');
      });
    })
    

    【讨论】:

    • 我在控制台中收到this.model.find is not a function
    • 是的,我没有走上正轨。但是get('firstObject') 是对的。我修改为:currentProfile: computed('profile', function(){return this.filterBy('name', get(this, 'profile')).get('firstObject');}),
    • 我不知道如何在 cmets 中做多行,但关键是使用 filterBy,但我从来没有像你建议的那样得到 firstObject。这似乎有效!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多