0

我正在为我的应用程序使用 Backbonejs,并且我正在尝试将 Facebook 评论和喜欢/发送按钮添加到我的一些视图中。我在实例化 Router 对象之前实例化 JavaScript SDK,如下所示:

// main.js  
require([ "Router" ], function (Router) {

// Facebook authentication
// Load the SDK Asynchronously
  (function(d){
     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.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));

  // Init the SDK upon load
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '302210766529054', // App ID
      channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

   // If the user has auth'd the app, instantiate the router object.
    FB.Event.subscribe('auth.statusChange', function(response) {
            if (response.authResponse) {
                 // user has auth'd your app and is logged into Facebook
                FB.api('/me', function(me){
                    if (me.username) {
                        window.currentUser = me;
                        $('#username').html(window.currentUser.name);
                        new Router();
                        Backbone.history.start();
                    }
                });
            }
    });

}

});

我可以轻松地windows.currentUser在路由器调用的任何视图中使用该变量。但是,当我添加

<div class="fb-comments" data-href="http://mywebsite/i/#items/" data-num-posts="2" data-width="470"></div>

对于路由器中视图调用的模板之一,不会出现评论(也不是喜欢/发送)div。当我将它添加到 index.html 文件的静态元素之一时,它工作正常。

我在这里错过了什么吗?

编辑

这是我的路由器,并在发布问题之前查看代码。

router.js
define(
[
'models/song',
'models/user',
'models/spotlight',
'models/user-playlist',
'views/spotlight-view',
'views/playlist-view',
'views/single-playlist-view',
],
function(Song, User, SpotlightModel,UserPlaylist, SpotlightView, PlaylistView, SinglePlaylistView){
    return Backbone.Router.extend({
        initialize: function(){
            window.songQue = new Array();

        },
        routes:{
            '': 'index',
            'spotlight': 'index',
            'playlists': 'allPlaylist',
            'playlists/:id':'onePlaylist',
        },

        index: function(){
            var spotlight = new SpotlightModel({id:1});
            var spotlightView = new SpotlightView({spotlight: spotlight});
            spotlight.fetch();
            $('.content').html(spotlightView.el);
        },

        allPlaylist: function(){
            $('.content').html('');
            var user = new User({id:window.currentUser.id});
            /*
             * once user details are fetched from the server,
             * loop through the arrays of their playlists and
             * render them in the template.
             */

            user.bind('change', function(){
                console.log(this);
                for(var i=0; i<this.get('objects')[0].playlists.length; i++){
                var playlistView = new PlaylistView({
                    playlist: user.get('objects')[0].playlists[i],
                    user: this.get('objects')[0]
                });
                $('.content').prepend(playlistView.el);
            }

            });
        },

        onePlaylist: function(playlist_id){
            var userPlaylist = new UserPlaylist({id:playlist_id});
            var singlePlaylistView = new SinglePlaylistView({playlist: userPlaylist});
            $('.content').html(singlePlaylistView.el);

        }
    });
}
   );



// single-playlist-view.js
define(
[
    'text!templates/single-playlist/single-playlist.html',
    'text!templates/single-playlist/single-playlist-details.html',
    'text!templates/playlist/song-row.html',
],
function(SinglePlaylistTemplate, SinglePlaylistDetailsTemplate, SongRowTemplate){
    return Backbone.View.extend({

        initialize:function(options){

            this.playlistTemplate = _.template(SinglePlaylistTemplate);
            this.playlistDetailsTemplate = _.template(SinglePlaylistDetailsTemplate);
            this.songRowTemplate = _.template(SongRowTemplate);
            this.playlist = options.playlist;

            $(this.el).prepend(this.playlistTemplate);
            this.playlist.bind('change', this.render, this);
            this.playlist.fetch();


        },

        render:function(){
            this.renderSongs();
            this.renderDetails();

        },

        renderSongs: function(){
            // Put the size of songs in a variable 
            var size = this.playlist.get('objects')[0].songs.length;
            // Extra loop just for the sake of filling the page.
            for(var j=0; j<5; j++){
                for(var i=0; i<size; i++){

                this.$("#song-row").prepend(this.songRowTemplate({
                    song: this.playlist.get('objects')[0].songs[i]
                }));
            }
            }
        },

        renderDetails: function(){
            this.$('.single-playlist-details').prepend(this.playlistDetailsTemplate({
                playlist: this.playlist,
                thumbnail: this.playlist.get('objects')[0].songs[0].song_thumbnail,
                total:this.playlist.get('objects')[0].songs.length,
            }));
        }

    });
}
  );
4

2 回答 2

0

我在@reach4thelasers 的帮助下解决了这个问题,我创建了一个从 Facebook 加载评论的 CommentsView,并在需要评论的每个视图中调用它。我只是在 CommentsViewFB.initinitialize函数中调用,所以代码看起来像这样:

initialize: function(){
            FB.init({
                    appId      : 'APP_ID', // App ID
                    channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
                    status     : true, // check login status
                    cookie     : true, // enable cookies to allow the server to access the session
                    xfbml      : true  // parse XFBML
            });

            this.commentsTemplate = _.template(CommentsTemplate);
                $(this.el).html(this.commentsTemplate({
                    comments_url: window.location.href
                }));
        }
于 2012-09-10T10:03:50.183 回答
0

主干视图将在 Facebook API 调用之后呈现 - 在您进行 Facebook 调用的阶段,该元素不存在。

你真的想在你想要显示 facebook API 结果的页面的路由中在你的路由器中进行 Facebook 调用。

如果您想在每个页面上显示它的内容 - 例如用户名/个人资料图片,请考虑将代码放在单独的函数中,例如 initUserInfoView(),然后从您要显示信息的每个路由中调用它。

老实说,这些东西实际上属于你的视图——路由器应该初始化并渲染视图,而视图负责从 Facebook 获取并显示

于 2012-07-12T09:03:32.590 回答