Dynamically determine existence of Ember component?

六月ゝ 毕业季﹏ 提交于 2019-12-13 15:54:42

问题


I was looking through the ember-htmlbars package and discovered this util for determining if a component is available? https://github.com/emberjs/ember.js/blob/master/packages/ember-htmlbars/lib/utils/is-component.js

Is there any way to use this from my app? I'm building a dashboard-type interface and some of the dashboard widgets have optional actions. In essence I'd like to do something like:

<div class="panel panel-default">
  <div class="panel-body">
    {{component model.displayComponent model=model}}
  </div>
  {{#if isComponent(model.actionComponent) }} <!-- this would be a property -->
    <div class="panel-footer">
      {{component model.actionComponent model=model}}
    </div>
  {{/if}}
</div>

My fallback is to put a blank action component for each of my widgets that don't have one, but it would be cleaner to be able to check to see if they exist first.


回答1:


you can create helper is-component

export default Ember.Helper.extend({
  compute([name]) {
    return this.container.registry.has('component:' + name);
  }
})

and use it like {{#if (is-component model.actionComponent) }}



来源:https://stackoverflow.com/questions/34795611/dynamically-determine-existence-of-ember-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!