我有这样的路线,
App.NewRoute = Ember.Route.extend({
setupController: function (controller,model) {
var self=this;
this.store.find('selectFill', { x: "20", y: "30" }).then(function (data) {
var usersList=self.GetUsersList(data); //usersList is always undefined even when GetUsersList returns the array.
controller.set('users',usersList);
});
},
GetUsersList: function (fillInfo) {
fillInfo.forEach(function (item) {
var users = item.get('users');
var y = users.get('content').map(function (entity) {
return entity.get('data');
});
return y;
});
}
});
现在,usersList始终未定义。我可以看到GetUsersList确实返回了一个数组,但usersList似乎没有接受它。 我在这做错了什么?
答案 0 :(得分:1)
关于从问题中返回GetUsersList
方法,您会遇到以下问题:
GetUsersList
应该返回一些东西。您错误地认为return
内的forEach
语句是GetUsersList
方法的返回实现目标的可能正确方法是:
GetUsersList: function (fillInfo) {
// Array to hold all the data
var returnVal = [];
fillInfo.forEach(function (item) {
// Inside forEach handler
var users = item.get('users');
var usersContentData = users.get('content').map(function (entity) {
// Inside map handler
return entity.get('data');
});
// No return here so the forEach doesn't interrupt and instead we append data to the returnVal array
returnVal.push(usersContentData);
});
// Return the array that contains items pushed inside forEach handler
return returnVal;
}
答案 1 :(得分:0)
App.NewRoute = Ember.Route.extend({
setupController: function (controller,model) {
var self=this;
this.store.find('selectFill', { x: "20", y: "30" }).then(function (data) {
var usersList=self.GetUsersList(data); //usersList is always undefined even when GetUsersList returns the array.
controller.set('users',usersList);
});
},
GetUsersList: function (fillInfo) {
var returnVal;
fillInfo.forEach(function (item) {
var users = item.get('users');
var y = users.get('content').map(function (entity) {
return entity.get('data');
});
returnVal=y;
});
return returnVal;
}
});
这确实奏效了。虽然奇怪,但是y在forEach循环中作了范围,但为什么函数返回undefined?