dimanche 15 novembre 2015

Manage Ember Multiple RSVP Promises on the same route

I am working with the GitHub API in order to load models in a specific route

I am doing two promises one for my personal github details : http://ift.tt/1N4ymNN and the other one for my GitHub repositories http://ift.tt/1MRzF2c

I can load the models separately but the problem is that i don't figure on how to load both models at the same time in my specific route.

See the code

var IndexRoute = Ember.Route.extend({
    model: function(params) {
        var url, self, git;
        self = this;
        git = this.store.createRecord('git',{});
        url = 'http://ift.tt/1N4ymNN';
        return new Ember.RSVP.Promise(function(resolve, reject) {
            return Ember.$.getJSON(url, function(data) {
                var item = [];
                git.setProperties({
                    name: data.name,
                    login: data.login,
                    location: data.location,
                    company: data.company,
                    followers: data.followers,
                    following: data.following
                });
                item.pushObject(git);
                return resolve(item);
            });
        });
    },
    model: function(params){
        var self, url, repoListProxy;
        self = this;
        url = 'http://ift.tt/1MRzF2c';
        repoListProxy = Ember.ArrayProxy.create({
            content: []
        });
        return new Ember.RSVP.Promise(function(resolve, reject) {
            return Ember.$.getJSON(url, function(repos) {
                if (repos.length) {
                    repos.toArray().forEach(function(item, index, arr){
                        var repo;
                        repo = self.createReposList(item, repoListProxy);
                    });
                    repos = repoListProxy.get('content');
                    return resolve(repos);
                }
            });
        });
    },
    createReposList: function(repo, arr){
        var record
        record = this.store.createRecord('repo',{}),
        record.setProperties({
            name: repo.name,
            description: repo.description
        })
        arr.pushObject(record);
        return record;
    },
});

How can i load these multiple models with Ember.RSVP.Promise in my specific route?

Aucun commentaire:

Enregistrer un commentaire