Question

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?

Était-ce utile?

La solution

Regarding the return of the GetUsersList method from the question you have the following issues:

  1. The GetUsersList should return something. You are wrongfully assuming that the return statement from inside the forEach is the return of the GetUsersList method
  2. The result of the map should be concatenated somehow so your program doesn't have activity without any result

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;
}

Autres conseils

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?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top