Emixjs从Mixin动态注入

时间:2018-11-27 06:03:12

标签: javascript ember.js ember-cli

我有很多使用差异服务的控制器,我应该扩展一个混合。在mixin中,基于控制器的调用,我应该注入差异服务。真的吗?如果是,我愿意提出任何建议。 我的控制器如下:

import FilterController from 'core/addon/filter/filter-controller';
import defaultMixin from 'default-mixin';

export default FilterController.extend(defaultMixin,{
  filter: Ember.inject.service(Pathes.inject.service.leaseFilter),
})

Mixin:

import Ember from 'ember';
import Views from '../const/views';
import ViewsService from '../static-services/views';

const {
  computed,
} = Ember;

export default Ember.Mixin.create({

  //THIS VIEW SERVICE I SHOULD INJECT BASED ON CONTROLLER
  //VIEW.INJECT.SERVICE.[LEASE,LEASE1,LEASE2,LEASE3]

  viewService: Ember.inject.service(Views.inject.service.Lease),

  defaultViewType: computed(
    'viewService.filteredViewsList',
    function () {
      const viewList = this.get('viewService.filteredViewsList');
      return ViewsService.getDefaultViewFromList(viewList);
    }
  ),
});

1 个答案:

答案 0 :(得分:1)

最直接的选择是覆盖控制器viewService中的extend

export default FilterController.extend(defaultMixin,{
  viewService: Ember.inject.service('someViewService'),
  filter: Ember.inject.service(Pathes.inject.service.leaseFilter),
})

这样,当您在mixin代码中使用它时,对this.get('viewService')的任何调用都将解析为您在ctrl中覆盖的任何内容。