I am trying to load multiple models with Ember $.getJSON. Initially i created the models objects successfully in separates routes but i need now to create them in the same route. I am using Github API
I came with the conclusion to use Ember.RSVP.hash
and that’s how i try to achieve it
I am getting two JSON
userurl = 'https://api.github.com/users/user';
reposurl = 'https://api.github.com/users/user/repos';
I create the model objects for the repositories and the user git information to find them later in the store
with Ember.RSVP.hash
Here the Route code ( sorry for the length )
model: function(params) {
var userurl, reposurl, self, git, repoListProxy, usersPromise, repositoriesPromise;
self = this;
git = this.store.createRecord('git',{});
userurl = 'https://api.github.com/users/user';
reposurl = 'https://api.github.com/users/user/repos';
repoListProxy = Ember.ArrayProxy.create({
content: []
});
usersPromise = function(){
return Ember.$.getJSON(userurl, 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);
};
repositoriesPromise = function(){
return Ember.$.getJSON(reposurl, 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);
}
});
};
return Ember.RSVP.hash({
git: this.store.find('git'),
repo: this.store.find('repo')
});
},
createReposList: function(repo, arr){
var record
record = this.store.createRecord('repo',{}),
record.setProperties({
name: repo.name,
description: repo.description
})
arr.pushObject(record);
return record;
},
Here my console log error
I can not really understand where it is my error but assume that i am not using Ember.RSVP.hash
as it should, or there is a better way to load these multiple models?