Access store from component

后端 未结 7 746
离开以前
离开以前 2020-12-08 04:40

i have a component and when user click on component it add some value to store,i try to use this way but i get an error :

OlapApp.MeasureListItemComponent =         


        
相关标签:
7条回答
  • 2020-12-08 04:43

    Since Ember 2.1.0

    export default Ember.Component.extend({
      store: Ember.inject.service('store'),
    });
    

    before Ember 2.1.0 - dependency injection way

    App.MyComponent = Ember.Component.extend({
    
      store: Ember.computed(function() {
         return this.get('container').lookup('store:main');
      })
    
    });
    

    before Ember 2.1.0 - controller way

    You can pass store as property from controller:

    App.MyComponent = Ember.Component.extend({
    
      value: null,
      store: null,
      tagName: "input",
    
      didInsertElement: function () {
    
         if (!this.get('store')) {
            throw 'MyComponent requires store for autocomplete feature. Inject as store=store'
         }
      }
    
    });
    

    Store is available on each controller. So in parent view you can include component as follows:

    {{view App.MyComponent
        store=store
        class="some-class"
        elementId="some-id"
        valueBinding="someValue"
    }}
    

    Passing properties to component is documented here

    0 讨论(0)
  • 2020-12-08 04:44

    The store can be injected with help of dependency injection.

    Example

    import Ember from 'ember';
    
    export default Ember.Component.extend({
    
      /**
       *
       */
      store: Ember.inject.service(),
    
      /**
       * Initialize the component.
       */
      init() {
        this.initialize();
    
        this._super();
      },
    
      /**
       * Initialize the properties and prerequisites.
       */
      initialize() {
        // Set the component properties
        this.todos().then((data) => {
          this.set('todoEntries', data);
        });
      },
    
      /**
       * Returns the todo entries.
       *
       * @returns {*|Promise|Promise.<T>}
       */
      todos() {
        const store = this.get('store');
    
        return store.findAll('todo');
      },
    
    });
    
    0 讨论(0)
  • 2020-12-08 04:50

    I don't know if components are intended to be used such a way. But if you want, I think you can declare an initializer and inject the store into all components.

    Ember.onLoad('OlaApp', function(OlaApp) {
      OlapApp.initializer({
        name: 'injectStoreIntoComponents',
        before: 'registerComponents',
        initialize: function(container, application){
          container.register('store:main', App.Store);
          container.injection('component', 'store', 'store:main');
        }
      })
    });
    

    Here is a contrived but working example: http://jsbin.com/AlIyUDo/6/edit

    0 讨论(0)
  • 2020-12-08 04:52

    Since Ember v1.10, the store can be injected to components using initializers, see: http://emberjs.com/blog/2015/02/07/ember-1-10-0-released.html#toc_injected-properties:

    export default Ember.Component.extend({
        store: Ember.inject.service()
    });
    
    0 讨论(0)
  • 2020-12-08 04:58

    The current ember-cli way to do this appears to be with an initializer. Very similar to the @Sly7_7 answer.

    To get a basic model use:

      ember g initializer component-store-injector
    

    Then edit this to:

    // app/initializers/component-store-injector.js
    
    export function initialize(container, application) {
      application.inject('component', 'store', 'store:main');
    }
    
    export default {
      name: 'component-store-injector',
      initialize: initialize
    };
    

    I believe this will add the store to all components.

    Stolen from https://github.com/ember-cli/ember-cli-todos

    0 讨论(0)
  • 2020-12-08 04:58

    Another way which no one has yet mentioned is to simply pass controller.store to the component e.g.

    {{my-awesome-component store=controller.store}}
    
    0 讨论(0)
提交回复
热议问题