我正在尝试为Travis-CI Mobile加载一些最喜欢的存储库我试图整理here
我拥有的是一系列存储库ID,如下所示:
var favoriteRepos = ["668498","557554","7934","207993"];
我们怎么能用ember-data revision 12,the Travis custom RESTAdapter和Travis API加载所有这些回购?
到目前为止,这是我尝试失败的原因:
// This is in the repo model - https://github.com/floydpink/Travis-CI-www/blob/master/js/app/models/Repo.js
Repo.reopenClass({
favorites : function (favorites) {
// favorites would be an array of repo-ids like ["451069","538603"]
var faves = Ember.ArrayProxy.create({
isLoadedBinding : 'content.isLoaded',
content : Ember.A([])
});
favorites.forEach(function (favorite) {
faves.pushObject(Repo.find(favorite));
});
return faves;
}
});
// And in FavoritesController
this.set('content', Repo.favorites(favoriteRepos));
所以通用的问题是,我们如何使用ember-data来加载id的几个不同的记录呢?
答案 0 :(得分:4)
你应该能够做到:
Repo.reopenClass({
favorites : function (favorites) {
// favorites would be an array of repo-ids like ["451069","538603"]
return Ember.ArrayProxy.createWithMixins({
content: favorites.map(function(id) { return Repo.find(id); }),
isLoaded: function() {
return this.everyProperty('isLoaded');
}.property('@each.isLoaded');
});
}
});
答案 1 :(得分:1)
如果您的把手模板如下所示:
{{#if isLoaded}}
{{#each controller}}
...
{{/each}}
{{/if}}
然后它将无效,因为您从未在阵列上将isLoaded
设置为true
。根据您使用的数据实现,您可以执行以下操作:
Repo.reopenClass({
favorites: function (ids) {
// ids would be an array of repo-ids like ["451069","538603"]
var loadCount = 0;
var favorites = Em.A();
ids.forEach(function(id) {
var favorite = Repo.find(id);
favorites.pushObject(favorite);
favorites.then(function() {
loadCount++;
if (loadCount == ids.length) {
favorites.set('isLoaded', true);
}
});
});
return favorites;
}
});
从服务器加载所有收藏夹后,isLoaded
属性设置为true。