2011-09-12 33 views
0

我在我的MVC應用程序中有一個局部視圖,我想要顯示一個Facebook評論框。我還希望將數據庫中的信息保存在用戶發佈的每條評論中 - 是的,我知道它們也可以在Facebook上看到,但我需要將它們保存在數據庫中。爲了這個目的,我寫了這段代碼(注意:這是我的部分視圖中的所有代碼,我試圖將它拆分爲更具可讀性,並且應用程序密鑰已正確插入。):通過FB.Event.subscribe來訂閱comment.create或comment.remove的方法不會觸發

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %> 
<div id="fb-root"> 
</div> 
<script type="text/javascript"> 
window.fbAsyncInit = function() 
{ 
    FB.init({ appId: 'MY_APP_KEY', status: true, cookie: true, xfbml: true }); console.log('fb init'); 
    FB.Event.subscribe('comment.create', 
     function (response) 
     { 
      console.log("comment created"); 
      var commentQuery = FB.Data.query("SELECT text, fromid FROM comment WHERE post_fbid='" + response.commentID + 
       "' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url='" + response.href + "')"); 
      var userQuery = FB.Data.query("SELECT name FROM user WHERE uid in (select fromid from {0})", commentQuery); 
      FB.Data.waitOn([commentQuery, userQuery], function() 
      { 
       var commentRow = commentQuery.value[0]; 
       var userRow = userQuery.value[0]; 
       console.log(userRow.name + " (id: " + commentRow.fromid + ") posted the comment: " + commentRow.text); 
       trackcomments(response['commentID'], response['href'], 'create', commentRow.text, userRow.name, commentRow.fromid); 
      }); 
     }); 
    FB.Event.subscribe('comment.remove', 
     function (response) 
     { 
      console.log("comment removed"); 
      trackcomments(response['commentID'], response['href'], 'remove', null, null, null); 
     }); 
}; 
(function() 
{ 
    var e = document.createElement('script'); 
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1'; 
    e.async = true; document.getElementById('fb-root').appendChild(e); 
}() 
    ); 

的trackcomments功能:

function trackcomments(_commentid, _address, _action, 
_commentMessage, _userName, _userId) 
{ 
    $.ajax({ 
     type: 'POST', 
     url: '/trackcomments', 
     data: 
      { 
       commentId: _commentid, 
       pageUrl: _address, 
       actionTaken: _action, 
       userName: _userName, 
       userId: _userId, 
       commentMessage: _commentMessage 
      }, 
     success: function (result) 
     { 
      console.log("successfully received callback"); 
     } 
    }); 
} 
</script> 

而Facebook的評論標籤,略低於上面的代碼:

<fb:comments notify="true" href="<%= Request.Url.ToString() %>" num_posts="3" width="630"></fb:comments> 

問題是每次發佈評論(或刪除)時,這些功能都不會觸發。我看到的唯一控制檯日誌文本是「fb init」。顯然,服務器上也沒有任何反應。

過去兩天我一直在撓頭,但我似乎無法找到答案 - 我會非常感謝任何能指引我走向正確方向的人。

謝謝!

回答

1

你應該把相關的FB的一切。[...]在

window.fbAsyncInit = function(){ 
    // Your code 
}); 

功能(FB.init,FB.Event.suscribe,...),否則你的代碼將在FB前解析js sdk已經完全加載,你永遠不會真的適合你的活動

+0

的FB代碼已經在window.fbAsyncInit塊... –

+0

嗯我不好對不起,不知道是什麼問題那麼。 我有一個類似的問題,當2 FB.init哪裏觸發在同一頁面上,事件不發送到良好的實例,但我不認爲這是在這裏的原因... – Olivier

0

你需要確保添加notify="true"標記,如果這不適合你,請確保你使用的是最新的fbComments插件。嘗試添加migrated="1"標籤下面,看看是否有所幫助:

<fb:comments notify="true" migrated="1"></fb:comments> 
+0

通知=「真」是已經在和遷移=「1」沒有幫助 –

0

我面臨着同樣的問題,今天,只是設法解決這個問題。我的問題是,我有2個不同的fbAsyncInit註冊。一旦我將他們結合起來,我就開始收到評論通知。

並且「notify = true」參數對獲取評論通知沒有必要。

錯誤:

window.fbAsyncInit = function(){ 
    // Init and Authenticate 
}); 

window.fbAsyncInit = function(){ 
    // Comment Event Subscribe. 
}); 

右:

window.fbAsyncInit = function(){ 
    // Init and Authenticate 
    // Comment Event Subscribe. 
}); 
相關問題