jQuery使用变量外部函数

时间:2014-11-20 23:06:51

标签: javascript jquery json

我使用jquery.feeds.js汇总RSS Feed并预处理使用jsonp.js收到的数据。问题是我无法在summarize以外的preprocess函数中设置变量$('#feed').feeds({ feeds: { reuters: 'http://feeds.reuters.com/reuters/businessNews' }, max: 2, preprocess: function ( feed ) { var articleLink = (this.link); var summarize = ''; $.getJSON({ url: 'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url='+articleLink+'&callback=?', corsSupport: true, jsonpSupport: true, success: function(data){ var summarize = data.summary } }); alert(summarize); this.contentSnippet = summarize }, entryTemplate: '<h3><!=title!></h3><p><!=contentSnippet!></p><i><!=link!></i>' }); 。我确实把它设置为一个通用变量,所以我不知道我可能做错了什么。我是否正在运行多个JSON请求?

我的代码:

{{1}}

JSFIDDLE

3 个答案:

答案 0 :(得分:1)

我认为你的意思是

$('#feed').feeds({
    feeds: {
        reuters: 'http://feeds.reuters.com/reuters/businessNews'
    },
    max: 2,
    preprocess: function ( feed ) {
        var articleLink = (this.link);
        var summarize = '';

        var that = this;

        $.getJSON({
            url: 'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url='+articleLink+'&callback=?',
            corsSupport: true,
            jsonpSupport: true,

            success: function(data){
                that.contentSnippet = data.summary
            }
        });

    },
    entryTemplate: '<h3><!=title!></h3><p><!=contentSnippet!></p><i><!=link!></i>'
});

答案 1 :(得分:1)

您有一系列错误未在其他帖子中解决..

  • preprocess回调允许在显示当前对象(Feed)之前进行更改 由于getJSON是一个ajax调用,它将得到结果太晚。即使在contentSnippet回调中更改success也无法解决此问题。
  • 您使用$.getJSON方法,就好像它是$.ajax一样。所以你传递了错误的参数。只需使用$.ajax作为语法
  • 最后要修复第一个问题,您需要稍微更改模板,以便稍后可以找到相关部分(当ajax请求完成时)并使用onComplete回调相反( of feeds plugin

所有更改一起提供

$('#feed').feeds({
    feeds: {
        reuters: 'http://feeds.reuters.com/reuters/businessNews'
    },
    max: 2,
    onComplete: function(entries){ // use onComplete which runs after the normal feed is displayed
        var $this = $(this);
        entries.forEach(function(entry){
            var $self = $this.find('.entry[data-link="'+entry.link+'"]');

            $.ajax({
                url:'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url='+entry.link,
                corsSupport: true, 
                jsonpSupport: true,     
                success: function(data){
                    // add the results to the rendered page
                    $self.find('.snippet').html( data.summary );
                }
              });
        });   
    }, // change the template for easier access through jquery
    entryTemplate: '<div class="entry" data-link="<!=link!>"><h3><!=title!></h3><p class="snippet"><!=contentSnippet!></p><i><!=link!></i></div>'
});

http://jsfiddle.net/gaby/pc7s2bmr/1/

演示

答案 2 :(得分:-1)

Mathletics是正确的。这样做......

&#13;
&#13;
$('#feed').feeds({
    feeds: {
        reuters: 'http://feeds.reuters.com/reuters/businessNews'
    },
    max: 2,
    preprocess: function ( feed ) {
        var articleLink = (this.link);
        var summarize = '';
      var _this = this;

        $.getJSON({
            url: 'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url='+articleLink+'&callback=?',
            corsSupport: true, 
            jsonpSupport: true,

            success: function(data){
               _this.contentSnippet = data.summary
            }
          });

          alert(summarize);
    },
    entryTemplate: '<h3><!=title!></h3><p><!=contentSnippet!></p><i><!=link!></i>'
});
&#13;
&#13;
&#13;