Reputation: 8436
I have a route like this,
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;
});
}
});
Now the usersList is always undefined. i can see that GetUsersList does indded return an array, but usersList doesnt seems to take it. What am i doing wrong here?
Upvotes: 0
Views: 195
Reputation: 594
Regarding the return of the GetUsersList
method from the question you have the following issues:
GetUsersList
should return something. You are wrongfully assuming that the return
statement from inside the forEach
is the return of the GetUsersList
method A possible right way to accomplish what you want would be like this:
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;
}
Upvotes: 1
Reputation: 8436
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;
}
});
this did work. weird though, y was scoped inside the forEach loop, but why the function return undefined?
Upvotes: 0