how to get the store in a component in ember.js

前端 未结 1 473
离开以前
离开以前 2021-01-18 02:46

How in the world do i get a handle on the store inside of a component? I\'m trying to create an auto-complete component that returns results from the store.



        
1条回答
  •  清酒与你
    2021-01-18 03:18

    The real answer is you shouldn't. A component should be agnostic of the outside world, and adding a dependency on the store breaks that concept. Really you should get the models beforehand (in the route, or controller, depending on the logic) and passing them into the component.

    https://github.com/emberjs/data/blob/master/TRANSITION.md

    In general, looking up models directly in a component is an anti-pattern, and you should prefer to pass in any model you need in the template that included the component.

    Now that I've said that, just pass the store into the component. It lives on the routes and controllers, so when you create a component send in the store as one of the arguments, then you can access it using this.get('store')

    {{auto-complete store=controller.store}}
    

    or

    {{auto-complete store=store}}
    

    http://emberjs.jsbin.com/OxIDiVU/720/edit

    0 讨论(0)
提交回复
热议问题