如果你想用 Meteor 构建一个完整的客户端应用程序,使用 Facebook JavaScript SDK 不是一个坏主意......
乍一看,Alex C 的答案很有效,但是FB.logout()
在“导航”并返回到<div id="fb-root"></div>
定义的“页面”之后我遇到了一些问题,因为当fb-root
重新渲染时,FB.logout() 停止工作。
所以我认为加载 Facebook JavaScript SDK 的最佳方法是使用模板创建的回调:
Template.fbLogin.created = function () {
if (!Session.get("is Facebook JDK loaded?")) {
Session.set("is Facebook JDK loaded?", true);
// https://developers.facebook.com/docs/reference/javascript/
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '[YOUR_APP_ID]', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code such as adding Event Listeners goes here
};
// Load the SDK's source Asynchronously
// Note that the debug version is being actively developed and might
// contain some type checks that are overly strict.
// Please report such bugs using the bugs tool.
(function(d, debug){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
}
};
FB.logout()
此外,要正常工作还需要做的另一件事是在<div id="fb-root"></div>
. 所以你的代码应该是这样的:
<body>
{{#constant}}
<div id="fb-root"></div>
{{/constant}}
<!-- rest of body... -->
我还发现有必要fb-root
在身体后立即放置。
在http://evee.meteor.com有一个像这样运行代码的实时应用程序,
你可以在这里找到它的源代码:https ://github.com/fjsj/evee/
(检查fbLogin.js和app. html和) _created
constant
fb-root
奖励(如何使用 Meteor 提供 channel.html 文件)
截至 2013 年 1 月,Meteor 不支持服务器端路由来提供静态 HTML。那么如何用 Meteor制作一个channel.html呢?将其放入 /public 将不起作用,因为 .html 文件由 Meteor 作为模板处理。
通过使用 Meteor 内部,这是可能的!正如这个答案所建议的,我们需要使用类似中间件的方法(由Connect提供支持)。只需将其放在您的 /server 上(注意它将在 yourapp.meteor.com/fb/channel.html 上提供):
// serve channel.html file
var connect = __meteor_bootstrap__.require("connect");
__meteor_bootstrap__.app
.use(connect.query())
.use(function(req, res, next) {
// Need to create a Fiber since we're using synchronous http
// calls and nothing else is wrapping this in a fiber
// automatically
Fiber(function () {
if (req.url === "/fb/channel.html") {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<script src="//connect.facebook.net/en_US/all.js"></script>');
} else {
// not an channel.html request. pass to next middleware.
next();
return;
}
}).run();
});
这也在生产中工作,代码可在 GitHub 上获得(包括缓存头)。