admin管理员组文章数量:1293149
How in the world do i get a handle on the store inside of a ponent? I'm trying to create an auto-plete ponent that returns results from the store.
App.AutoCompleteComponent = Ember.Component.extend({
//-------------------------------------------
// Ember Properites
//-------------------------------------------
content: Ember.ArrayController.create(),
//-------------------------------------------
// Instance Properties
//-------------------------------------------
queryText: "",
ponentItemSelected: null,
//-------------------------------------------
// Observers
//-------------------------------------------
queryTextChanged: function () {
this.updateContent(this.get("queryText"));
}.observes("queryText"),
//-------------------------------------------
// Instance Methods
//-------------------------------------------
selectItem: function (item) {
this.set("ponentItemSelected", item);
},
updateContent: function (queryText) {
if (queryText.length <= 5) {
console.log('not greater than 5 chars');
return;
}
this.get("content").setObjects([]);
var items = App.Company.find();
this.get("content").setObjects(items);
}
});
here is my pany model
App.Company = DS.Model.extend({
name: DS.attr('string'),
created_at: DS.attr('date'),
updated_at: DS.attr('date'),
people: DS.hasMany('person')
});
I've tried:
this.get('store')
DS.Store.find('pany')
- just
store
App.Company.find()
I always get an Uncaught TypeError ... has no method 'find'
How in the world do i get a handle on the store inside of a ponent? I'm trying to create an auto-plete ponent that returns results from the store.
App.AutoCompleteComponent = Ember.Component.extend({
//-------------------------------------------
// Ember Properites
//-------------------------------------------
content: Ember.ArrayController.create(),
//-------------------------------------------
// Instance Properties
//-------------------------------------------
queryText: "",
ponentItemSelected: null,
//-------------------------------------------
// Observers
//-------------------------------------------
queryTextChanged: function () {
this.updateContent(this.get("queryText"));
}.observes("queryText"),
//-------------------------------------------
// Instance Methods
//-------------------------------------------
selectItem: function (item) {
this.set("ponentItemSelected", item);
},
updateContent: function (queryText) {
if (queryText.length <= 5) {
console.log('not greater than 5 chars');
return;
}
this.get("content").setObjects([]);
var items = App.Company.find();
this.get("content").setObjects(items);
}
});
here is my pany model
App.Company = DS.Model.extend({
name: DS.attr('string'),
created_at: DS.attr('date'),
updated_at: DS.attr('date'),
people: DS.hasMany('person')
});
I've tried:
this.get('store')
DS.Store.find('pany')
- just
store
App.Company.find()
I always get an Uncaught TypeError ... has no method 'find'
1 Answer
Reset to default 12The real answer is you shouldn't. A ponent 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 ponent.
https://github./emberjs/data/blob/master/TRANSITION.md
In general, looking up models directly in a ponent is an anti-pattern, and you should prefer to pass in any model you need in the template that included the ponent.
Now that I've said that, just pass the store into the ponent. It lives on the routes and controllers, so when you create a ponent send in the store as one of the arguments, then you can access it using this.get('store')
{{auto-plete store=controller.store}}
or
{{auto-plete store=store}}
http://emberjs.jsbin./OxIDiVU/720/edit
本文标签: javascripthow to get the store in a component in emberjsStack Overflow
版权声明:本文标题:javascript - how to get the store in a component in ember.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741574680a2386213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论