我在下面的代码中遇到两个错误。我不确定如何修复它。看起来#each正在寻找一个承诺,而不是模型或数组。我对吗?如果是,我怎么能让它采取数组或模型?感谢。
错误1. 断言失败:#each循环的值必须是 阵列。您传递了[object Object] ember.js:3231
错误2. 未捕获TypeError:对象[object Object]没有方法 'addArrayObserver'
var StudentBooksController = Ember.Controller.extend({
needs: ['application','request'],
selectedBooks: function() {
//return an array here such as [1, 3, 5, 7]
}.property(),
studentList: function() {
return this.store.find('student');
}.property(),
filteredStudentBooks: function() {
var self = this;
// I tried "return []" here and no error on the console.
return self.get('studentList').then(function (students){
// I tried "return []" here and it still gave me the same error on console.
return self.get('selectedBooks').then(function(sb){
var aList = students.get('content');
aList.forEach(function(al){
al.set('hasAccess', _.contains(sb, sb.get('id') ));
console.log(_.contains(sb, sb.get('id') ));
});
return aList;
});
})
}.property('selectedBooks','studentList')
});
export default StudentBooksController;
//----- Nothing in the Route file ---
{{#each filteredStudentBooks}}
// do something here such as printing the name
{{name}}
{{#if hasAccess}}
User has access.
{{else}}
User doesn't have access.
{{/if}}
{{/each}}
在@ kingpin2k的帮助下,这是我最终的解决方案。
filteredStudentBooks: function() {
var self = this,
resp = [];
self.get('studentList').then(function (students){
// I tried "return []" here and it still gave me the same error on console.
self.get('selectedBooks').then(function(sb){
var aList = students.get('content');
var sortedStudentList = aList.sortBy('name');
aList = sortedStudentList ;
aList.forEach(function(al){
al.set('hasAccess', _.contains(sb, sb.get('id') ));
console.log(_.contains(sb, sb.get('id') ));
resp.pushObject(al);
});
});
})
return resp;
}.property('selectedBooks.[]','studentList.[]')
答案 0 :(得分:2)
你正在返回一个承诺而不是一个数组,另外你可能应该正在观察数组的变化,而不仅仅是属性。
filteredStudentBooks: function() {
var self = this,
resp = [];
self.get('studentList').then(function (students){
// I tried "return []" here and it still gave me the same error on console.
self.get('selectedBooks').then(function(sb){
var aList = students.get('content');
aList.forEach(function(al){
al.set('hasAccess', _.contains(sb, sb.get('id') ));
console.log(_.contains(sb, sb.get('id') ));
});
resp.pushObjects(aList.sortBy('name').toArray());
});
})
return resp;
}.property('selectedBooks.[]','studentList.[]')
答案 1 :(得分:0)
这是我的问题的工作解决方案。 @ kingpin2k 获得了大部分帮助。我修改了代码以获得排序和输出以返回模板。
filteredStudentBooks: function() {
var self = this,
resp = [];
self.get('studentList').then(function (students){
// I tried "return []" here and it still gave me the same error on console.
self.get('selectedBooks').then(function(sb){
var aList = students.get('content');
var sortedStudentList = aList.sortBy('name');
aList = sortedStudentList ;
aList.forEach(function(al){
al.set('hasAccess', _.contains(sb, sb.get('id') ));
console.log(_.contains(sb, sb.get('id') ));
resp.pushObject(al);
});
});
})
return resp;
}.property('selectedBooks.[]','studentList.[]')