Troede lige at få det til at virke. Mange tak til Dave og emgee for jeres hjælp! Nedenfor er koden for dem, der har det samme problem. Lad mig også vide, om der er en bedre måde at gøre dette på.
Serverkode:
function Facebook(accessToken) {
this.fb = Meteor.require('fbgraph');
this.accessToken = accessToken;
this.fb.setAccessToken(this.accessToken);
this.options = {
timeout: 3000,
pool: {maxSockets: Infinity},
headers: {connection: "keep-alive"}
}
this.fb.setOptions(this.options);
}
Facebook.prototype.query = function(query, method) {
var self = this;
var method = (typeof method === 'undefined') ? 'get' : method;
var data = Meteor.sync(function(done) {
self.fb[method](query, function(err, res) {
done(null, res);
});
});
return data.result;
}
Facebook.prototype.getUserData = function() {
return this.query('me/photos');
}
Meteor.methods({
getUserData: function() {
var fb = new Facebook(Meteor.user().services.facebook.accessToken);
var data = fb.getUserData();
_.forEach(data.data, function(photo) {
Photos.insert(photo, function(err) {
if(err) console.error(err);
});
});
}
});
Meteor.publish('picture', function() {
return Photos.find();
});
Samlinger:
Photos = new Meteor.Collection('picture');
Klient:
Meteor.subscribe('picture');
Template.facebookphoto.helpers({
pictures: function () {
return Photos.find();
}
});
Template.fbgraph.events({
'click #btn-user-data': function(e) {
Meteor.call('getUserData', function(err, data) {
$('#result').text(EJSON.stringify(data, undefined, 4));
});
}
});
HTML-skabeloner:
<template name="fbgraph">
<div id="main" class="row-fluid">
{{> facebookphoto}}
</div>
<button class="btn" id="btn-user-data">Get User Data</button>
<div class="well">
<pre id="result"></pre>
</div>
</template>
<template name="facebookphoto">
<div class="photos">
{{#each pictures}}
{{> photoItem}}
{{/each}}
</div>
</template>
<template name="photoItem">
<div class="photo">
<div class="photo-content">
<a href="{{source}}">
<img class="img-rounded" src="{{picture}}">
</a>
</div>
</div>
</template>
{{#each pictures}} {{> photoItem}} {{/each}}