如果字段为空或填写JSON / jQuery不显示

时间:2012-03-14 08:38:22

标签: jquery facebook facebook-wall

我正在创建一个Facebook墙页面,我不希望在我的Feed中显示这些事件。

事件在JSON中包含以下字段:

  • 消息
  • 故事
  • 图标
  • 动作

消息包含:

  • 消息
  • 图片
  • link

所以区别在于它添加了一个字段故事的事件。 现在我想过滤掉这些帖子,但你怎么能说'如果填写了现场故事,请不要显示帖子'

我对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);

2 个答案:

答案 0 :(得分:1)

您想要检查数组中是否定义了故事,为此您使用typeof函数。

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

答案 1 :(得分:0)

此检查应删除所有有故事的帖子:

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