2017-11-04 119 views
0

我想构建一个显示所有匹配数据的表组件。我不知道如何让这个工作。Ember.js - 匹配模型数据,并在控制器中有很多关系

我有多个有多个市场的平台。

的模式很简单:

model() { 
    return this.store.findAll('platform', {include: 'markets'}); 
} 

我可以显示复选框,以便用户可以选择什么样的平台来比较并访问ID的控制器。

我该如何着手从控制器模型中获取正确的记录?我无法在路线中执行此操作,因为它取决于所选平台。

我可以用灰烬数据:

this.get('store').findRecord('platform', id, {include: 'markets'}) 

但我无法弄清楚如何进入市场。

我试图枚举接口还可以,但同一个问题:

this.get('model').filterBy('id', id) 

在此之后,什么是干净的方式来获得基于其名称匹配的市场?

回答

1

针对您的情况,您可以根据组件中的任何选定平台访问和比较市场。每个平台都应该在模型文件中建立与其相关市场的关系。这种关系可以让你从平台上进入市场。例如,模板内的控制器,组件或{{platform.markets}}内的platform.get('markets')。有关工艺粗糙实现整个应用程序文件的一些详细信息:

//Within the platform.js model just to set the basic relationship 
markets: hasMany(), 

//Within your controller.js build an array of selected platforms 
selectedPlatforms: null, 

actions: { 
    selectPlatform(platform) { 
    this.get('selectedPlatforms').pushObject(platform); 
    }, 
} 

//Within your template.hbs provide your component the array 
{{matching-markets platforms=selectedPlatforms}} 

//Within your component.js compare the platform markets 
platforms: null, 

matchingMarkets: computed('platforms', function() { 
    const platform1Markets = this.get('platforms.firstObject.markets'); 
    const platform2Markets = this.get('platforms.lastObject.markets'); 
    return /* Compare platform1Markets against platform2Markets */; 
}), 

// Within the component.hbs 
{{#each matchingMarkets as |market|}} 
    {{market.name}} 
{{/each}} 

请参考以下链接到EmberTwiddle看到一个粗略(略哈克)的例子,可能会提供一些更好的见解: https://ember-twiddle.com/364d9c04d37593f4a7c40cccf065a8fc?openFiles=routes.application.js%2C