简体   繁体   中英

Ember.js: route returning multiple models including a promise, gives error

I am trying to return multiple models for a route, and, yes, I am using RSVP.Hash ( GitHub link ):

App.GamesIndexRoute = Ember.Route.extend({
    model: function () {
        return new Ember.RSVP.Hash({
            player: App.LocalPlayer.singleton(this.store),
            games: [{id: 1, name: "Game 1"}, {id: 2, name: "Game 2"}]
        });
    },

    setupController: function(controller, models) {
        this._super(controller, models);
        controller.set("player", models.player);
    }
});

The problem is, one of the objects is coming from a promise itself as I want to find only the first object from that model ( GitHub link ):

App.LocalPlayer = DS.Model.extend({
    name: DS.attr("string"),
    playerId: DS.attr("string")
});

App.LocalPlayer.singleton = function (store) {
    return new Ember.RSVP.Promise(function (resolve, reject) {
        store.find("localPlayer").then(function (things) {
            var p = things.get("firstObject");
            if(!p) {
                p = store.createRecord("localPlayer");
            }
            resolve(p);
            reject({error: "Error loading LocalPlayer"});
        });
    });
};

When I try and go to the games.index route after having created a LocalPlayer object, it says: "Error while processing route: games.index undefined is not a function TypeError: undefined is not a function"

The repo is on GitHub here - totally open source. Please help.

Hahaha.... I realised my mistake. It was my fault for not reading properly, and it made me lost about 2 days of my life. Oh well.

RSVP.Hashes are initialized like this:

return Ember.RSVP.hash({ ... })

NOT like I was doing:

return new Ember.RSVP.Hash({ ... })

Problem solved. I wish the error reporting was as bit more clearer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM