我的 Ember.js 模型/ Controller 遇到一些问题。
我的代码生成此错误:
Uncaught TypeError: this.get(...).toArray is not a function(…)
我有这样的结构:
router.js
Router.map(function() {
this.route('search', function() {
this.route('detail', { path: '/:search_id' });
this.route('ranks', { path: '/:search_id/ranks' });
this.route('tweets', { path: '/:search_id/tweets' });
});
});
export default Router;
route/search.js
import Ember from 'ember';
export default Ember.Route.extend({
});
route/search/rank.js
export default Ember.Route.extend({
model(params) {
return this.get('store').findRecord('search', params.search_id);
},
setupController: function(controller, model) {
controller.set('model', model);
}
});
app/models/rank.js
export default DS.Model.extend({
date: DS.attr(),
tag: DS.attr('string'),
rank: DS.attr('number'),
freq: DS.attr('number'),
search: DS.belongsTo('search')
});
app/models/search.js
export default DS.Model.extend({
uniqueid: DS.attr('string'),
idate: DS.attr('date'),
fdate: DS.attr('date'),
keywords: DS.attr(),
ranks: DS.hasMany('rank')
});
app/controllers/search.js
export default Ember.Controller.extend({
});
app/controllers/search/rank.js
export default Ember.Controller.extend({
mydata: Ember.computed('model', function() {
this.get('model').forEach(function(item) {
console.log(item);
});
});
});
最佳答案
您并不是唯一一个在这个问题上挣扎的人,因为我最近看到了大量的问题,这些问题说明了我认为被忽略的关于 ember-data 的一点。
您调用 this.store.findRecord('search', params.search_id)
,然后您的 api 根据您的评论返回以下 JSON,其中包含 1 个 search
对象和 4 排名
{
"searches":[
{ "id":90,"uniqueid":"search_all","idate":"2016-12-01","fdate":"2016-12-10","keywords":""}
], "ranks":[
{"id":1,"rank":1,"tag":"referendum","date":"2016-12-05","freq":609,"search":"90"},
{"id":2,"rank":1,"tag":"referendum","date":"2016-12-06","freq":1487,"search":"90"},
{"id":3,"rank":1,"tag":"referendum","date":"2016-12-04","freq":242,"search":"90"},
{"id":4,"rank":1,"tag":"referendum","date":"2016-12-07","freq":191,"search":"90"}
]
}
现在,如果您执行了 ajax 请求,则整个 json blob 将可供您使用。但是,因为这是一个 ember 数据请求,而不是一个普通的 ajax 请求,所以您只会返回您查询的单个搜索实例。
但这不是问题,因为返回的搜索对象具有对您也获得的项目进行排名的链接。您可以调用 search.get('ranks')
并获取所有加载的排名。
解决方案:
我建议的解决方案是收集模型中所需的排名并将其传递给 Controller 。这样,如果您想独立于搜索加载它们,它就可以工作
路线/搜索/rank.js
export default Ember.Route.extend({
model(params) {
return this.get('store').findRecord('search', params.search_id).then((search) => {
// Find record only returns the search object. However, that search object has easy
// access to all of the previously retrieved rank objects
return Ember.RSVP.hash({
search: search,
ranks: search.get('ranks') // <= key difference here
});
});
},
setupController: function(controller, model) {
controller.set('search', model.search);
controller.set('myRanks', model.ranks);
}
});
app/controllers/search/rank.js
export default Ember.Controller.extend({
// ranks are available to you here as myRanks
});
app/views/search/rank/index.hbs
{{#each myRanks as |rank|}}
<h1>{{rank.rank}}</h1>
{{/each}}
关于javascript - 未捕获的 TypeError : this. get(...).toArray 不是函数(...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41005091/