admin管理员组文章数量:1391964
I'm trying to fetch albums list from facebook via JavaScript and Graph API. I'm doing it like this:
FB.api('/me/albums', function(response){
//
});
Album covers ing back as photo id, not a url that I can insert into html. So, I need to get a urls in second step. But, by application design, I can't get urls later and insert images. I need to return a plete data object with album propereties (id, name, cover url). My last try is listed below:
FB.api('/me/albums', { fields : 'id,name,count,cover_photo' }, function(response){
if (!response || response.error) {
callback.call(false);
return;
};
var parsed = $(response.data).map(function(){
if (this.count > 0) {
var result = {
aid : this.id,
title : this.name,
cover : this.cover_photo
};
return result;
};
});
for (var i = 0; i < parsed.length; i++) {
(function(i){
FB.api('/' + parsed[i].cover, { type : 'album' }, function(response){
parsed[i].thumb = response.picture;
});
})(i);
};
callback.call(parsed);
});
The problem is that the loop is not waiting for cover queries and going forward, leaving return object without cover urls. I think it's can not be so difficult to do, but I can't find the solution long enough. Any ideas are wele.
I'm trying to fetch albums list from facebook via JavaScript and Graph API. I'm doing it like this:
FB.api('/me/albums', function(response){
//
});
Album covers ing back as photo id, not a url that I can insert into html. So, I need to get a urls in second step. But, by application design, I can't get urls later and insert images. I need to return a plete data object with album propereties (id, name, cover url). My last try is listed below:
FB.api('/me/albums', { fields : 'id,name,count,cover_photo' }, function(response){
if (!response || response.error) {
callback.call(false);
return;
};
var parsed = $(response.data).map(function(){
if (this.count > 0) {
var result = {
aid : this.id,
title : this.name,
cover : this.cover_photo
};
return result;
};
});
for (var i = 0; i < parsed.length; i++) {
(function(i){
FB.api('/' + parsed[i].cover, { type : 'album' }, function(response){
parsed[i].thumb = response.picture;
});
})(i);
};
callback.call(parsed);
});
The problem is that the loop is not waiting for cover queries and going forward, leaving return object without cover urls. I think it's can not be so difficult to do, but I can't find the solution long enough. Any ideas are wele.
Share Improve this question asked Aug 14, 2012 at 9:13 WarGasmWarGasm 3273 silver badges12 bronze badges2 Answers
Reset to default 6window.getAlbums = function() {
FB.api({
method: 'fql.multiquery',
queries: {
query1: 'select aid,name,link,photo_count,cover_object_id from album where owner = me()',
query2: 'SELECT pid,src FROM photo WHERE object_id IN (SELECT cover_object_id FROM #query1)'
}
},
function(response) {
var parsed = new Array();
$(response[0].fql_result_set).each(function(index,value){
var result = {
aid : value.aid,
title : value.name,
cover : response[1].fql_result_set[index].src,
link :value.link
};
parsed.push(result);
})
getdata(parsed);
});
};
Apped your album detail using
function getdata(data){
$(data).each(function(index,value){
// console.log(value.aid + ' - '+ value.cover+ ' - '+ value.title );
$("#fb_albumb").append('<h3>'+ value.title +'</h3><a href="'+ value.link +'" target="_blank" ><img src="'+ value.cover +'" title="'+ value.title +'" /></a><br/>');
})
}
HTML
<fb:login-button scope="user_photos" onlogin="getAlbums()">Grant Permissions to Allow access to Photos and Albums</fb:login-button>
May be this will help you.
The problem is that the loop is not waiting for cover queries and going forward, leaving return object without cover urls.
Of course, because FB.api is an asynchronous method.
I think it's can not be so difficult to do, but I can't find the solution long enough.
You’d have to use a callback function for your second FB.api calls in the loop as well, and implement a counter in it – when this counter shows that all the requests are done, then you can pass the data on to somewhere else (or use it right there in that function, because “returning” it will not work – because the method is asynchronous).
Maybe better to use FQL right away, because there you could kind of “join” the necessary data together and get it all in just one call; something like a multiquery as f.e.
{
"q1" : "SELECT aid, name, cover_object_id FROM album WHERE owner = me()",
"q2" : "SELECT aid, src_big FROM photo WHERE object_id IN
(SELECT cover_object_id FROM #q1)"
}
(Test this query in the Graph API explorer.)
本文标签: javascriptGetting albums covers with facebook apiStack Overflow
版权声明:本文标题:javascript - Getting albums covers with facebook api - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744759111a2623643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论