Skip to content

Instantly share code, notes, and snippets.

@beautyfree
Forked from anonymous/derji
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beautyfree/d6d59c3469d7cf14b74b to your computer and use it in GitHub Desktop.
Save beautyfree/d6d59c3469d7cf14b74b to your computer and use it in GitHub Desktop.
.factory('feed', function($http, vk, $localStorage){
var owner_id = '-34882023';
var version = '5.34';
var count = '5';
var items = [];
var liked;
var isLiked = function(itemId){
return vk.call('likes.isLiked', {item_id: itemId, owner_id: owner_id, access_token: $localStorage.token, type: 'post', v: version}).then(function(result){
return result;
}, function(err){console.log('err' + err)});
};
return {
getFeed: function(offset){
// Вызываем асинхронный запрос к апи, который вернет промис
return vk.call('wall.get', {owner_id: owner_id, offset: offset, count: count, v: version}).then(function(res){
console.log(res);
items = res.data.response.items;
// Перебираем массив айтемс получая массив измененных айтемов
var promises = items.map(function(item) {
// Возвращаем модифицированный айтем в мэп
return isLiked(item.id).then(function(ress){
if(ress.data.response.liked == true){
console.log(item);
}
// Модифицированный айтем либо такой же
return item;
});
});
// Возвращаем промис с результатами когда отработают все промисы в массиве
return $q.all(promises);
}, function(err){
console.log('cant call');
});
},
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment