2012-03-14 56 views
1

我正在創建Facebook牆頁面,並且我不希望事件顯示在我的提要中。如果字段爲空或填充JSON/jQuery不顯示

的事件具有在JSON下列域:

  • 消息
  • 故事
  • 圖標
  • 動作
  • 類型

一個消息有:

  • 消息
  • 圖片
  • 鏈接

所以,不同的是它添加一個事件是一個場故事。 現在我想過濾掉這些帖子,但你怎麼能說'如果現場的故事是填補,不要顯示帖子'

我不是很有經驗的JavaScript,你可以看到。我用這個教程:click

(function($){ 

    $.fn.facebookWall = function(options){ 

     options = options || {}; 

     if(!options.id){ 
      throw new Error('You need to provide an user/page id!'); 
     } 

     if(!options.access_token){ 
      throw new Error('You need to provide an access token!'); 
     } 

     // Default options of the plugin: 

     options = $.extend({ 
      limit: 15 // You can also pass a custom limit as a parameter. 
     },options); 

     // Putting together the Facebook Graph API URLs: 

     var graphUSER = 'https://graph.facebook.com/'+options.id+'/?fields=name,picture&access_token='+options.access_token+'&callback=?', 
      graphPOSTS = 'https://graph.facebook.com/'+options.id+'/posts/?access_token='+options.access_token+'&callback=?&date_format=U&limit='+options.limit; 

     var wall = this; 

     $.when($.getJSON(graphUSER),$.getJSON(graphPOSTS)).done(function(user,posts){ 

      // user[0] contains information about the user (name and picture); 
      // posts[0].data is an array with wall posts; 

      var fb = { 
       user : user[0], 
       posts : [] 
      }; 

      $.each(posts[0].data,function(){ 

       // We only show links and statuses from the posts feed: 


       if ((this.story == '') || !this.message){ 
        return true; 
       } 


       // Copying the user avatar to each post, so it is 
       // easier to generate the templates: 
       this.from.picture = fb.user.picture; 

       // Converting the created_time (a UNIX timestamp) to 
       // a relative time offset (e.g. 5 minutes ago): 
       this.created_time = relativeTime(this.created_time*1000); 

       // Converting URL strings to actual hyperlinks: 
       this.message = urlHyperlinks(this.message); 

       fb.posts.push(this); 
      }); 

      // Rendering the templates: 
      $('#headingTpl').tmpl(fb.user).appendTo(wall); 

      // Creating an unordered list for the posts: 
      var ul = $('<ul>').appendTo(wall); 

      // Generating the feed template and appending: 
      $('#feedTpl').tmpl(fb.posts).appendTo(ul); 
     }); 

     return this; 

    }; 

    // Helper functions: 

    function urlHyperlinks(str){ 
     return str.replace(/\b((http|https):\/\/\S+)/g,'<a href="$1" target="_blank">$1</a>'); 
    } 

    function relativeTime(time){ 

     // Adapted from James Herdman's http://bit.ly/e5Jnxe 

     var period = new Date(time); 
     var delta = new Date() - period; 

     if (delta <= 10000) { // Less than 10 seconds ago 
      return 'Zo net '; 
     } 

     var units = null; 

     var conversions = { 
      millisecond: 1,  // ms -> ms 
      seconden: 1000,  // ms -> sec 
      minuten: 60,   // sec -> min 
      uur: 60,   // min -> hour 
      dag: 24,   // hour -> day 
      maand: 30,   // day -> month (roughly) 
      jaar: 12   // month -> year 
     }; 

     for (var key in conversions) { 
      if (delta < conversions[key]) { 
       break; 
      } 
      else { 
       units = key; 
       delta = delta/conversions[key]; 
      } 
     } 

     // Pluralize if necessary: 

     delta = Math.floor(delta); 
     if (delta !== 1) { units += 'en'; } 
     return [delta, units, "geleden"].join(' '); 

    } 

})(jQuery); 

回答

1

你要檢查,如果故事是在陣列中定義的,要做到這一點,你使用typeof功能。

if (typeof this.story == 'undefined')

+0

謝謝你,但我怎麼能說我只希望沒有故事的消息。 if((typeof this.story =='undefined')||!this.message){ return true; } 現在只顯示有故事的帖子。對不起,我是一個真正的菜鳥。 – user1268465 2012-03-14 08:58:13

0

這一檢查應該消除與故事的所有帖子:

if(this.story === undefined || this.story == null) { 
    // Non Story 
} else { 
    // Story 
} 
+0

我嘗試過這個,但我無法理解。如果你不使用return true,它不會顯示任何消息。如果返回true,它仍然只顯示事件。 – user1268465 2012-03-14 09:20:08

+0

謝謝,我已經明白了! – user1268465 2012-03-14 09:40:37