简体   繁体   English

如何将商店注入我的余烬组件

[英]How to inject the Store to my ember component

I am in the situation where i need to inject the store to my component , at least i think this is my need. 我处于需要将商店注入我的组件的情况,至少我认为这是我的需要。

This is the situation: 这种情况:

I have component , the code itself does not really matter but i paste it for better understanding: 我有component,代码本身并不重要,但是我将其粘贴以更好地理解:

//components/masonry-plugin.js
import Ember from 'ember';

export default Ember.Component.extend({
  didInsertElement : function(){
    this._super();
    Ember.run.scheduleOnce('afterRender', this, this.afterRenderEvent);
  },

  afterRenderEvent : function(){
    var $grid = this.$('.grid').masonry({
      itemSelector: '.grid-item',
      percentPosition: true,
      columnWidth: '.grid-sizer'
    });
    // layout Isotope after each image loads
    $grid.imagesLoaded().progress( function() {
      $grid.masonry();
    });  
  }
});

This is his own template 这是他自己的模板

<div class="grid">
    <div class="grid-sizer"></div>
    {{#each model}}
        <div class="grid-item">
          <img {{bind-attr src=imgLink}}>
        </div>
     {{/each}}
</div>

This template is imported in an other template photography.hbs by {{masonry-plugin}} 该模板由{{masonry-plugin}}导入到其他模板photography.hbs中。

The question is, since in photography.hbs i have access to the module imgLink , because in the route photography.js i create the module consuming the Flickr API, how can i make the module accessible also to the template in {{masonry-plugin}} ? 问题是,由于我在photography.hbs中可以访问模块imgLink ,因为在路线photography.js中我创建了使用Flickr API的模块,因此如何使该模块也可以访问{{masonry-plugin}}

Hope the explanation is clear 希望解释清楚

Here also the model img for clarification 这也是模型img的澄清

var img = DS.Model.extend({
    imgLink: DS.attr('string')
});

You need only one line in component to inject store or other services there: 您只需要将组件中的一行插入那里的商店或其他服务:

import Ember from 'ember';
const { service } = Ember.inject;

export default Ember.Component.extend({
    store: service(),
    foo: service('store') // if you dont want to name property same as service
});

First, for this you don't need the store in your component. 首先,为此,您不需要在组件中存储。

Just pass the img Model instance or the imgLink string to your component: 只需将img Model实例或imgLink字符串传递给您的组件即可:

{{masonry-plugin model=model}} 

(this depends on you having img available on your calling context) (这取决于您在调用上下文中是否有可用的img

If this does not help try to provide a simple example with an ember-twiddle or at least the code of your photography route , controller and template. 如果这样做没有帮助,请尝试提供一个简单的示例,其中包括余烬photography routecontroller和模板的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM