Access store from component

后端 未结 7 789
离开以前
离开以前 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

提交回复
热议问题