2014-11-20 63 views
-2

我使用jquery.feeds.js来聚合rss订阅源并预处理与jsonp.js一起收到的数据。问题是我不能使用我在preprocess函数外设置的变量summarize。我确实将它设置为一个通用变量,但我不知道我会做错什么。这可能是我运行多个JSON请求的问题吗?jQuery使用变量外函数

我的代码:

$('#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>' 
}); 

而一个JSFIDDLE

+0

哪个变量? – Mathletics 2014-11-20 23:11:59

+0

'汇总'变量 – babusi 2014-11-20 23:12:31

+0

你想用它做什么? – Mathletics 2014-11-20 23:13:06

回答

1

您有一系列未在其他职位寻址错误..

  • preprocess回调允许在当前对象的变化(饲料)才显示。
    由于getJSON是ajax调用,它会得到的结果为时已晚。即使在success回调中更改contentSnippet也不能解决此问题。
  • 您使用$.getJSON方法,就好像它是$.ajax一样。所以你通过它错误的论点。只需使用$.ajax为你的语法
  • 终于解决了第一个问题,你需要改变你的模板了一点,所以你可以找到相关的部分以后(当Ajax请求完成),并使用onComplete回调,而不是(提要插件

的所有变化一起给

$('#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/

+0

非常感谢您花时间研究您的答案。对此,我真的非常感激。这工作完美 – babusi 2014-11-21 00:00:27

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>' 
}); 
+0

是的...谢谢你是我的意思 – 2014-11-20 23:22:32

-1

Mathletics是正确的。做到这一点...

$('#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>' 
 
});

+0

'$ this'是什么? – 2014-11-20 23:18:28

+0

我刚刚尝试过将'$ this'编辑为'this',但它不起作用 – babusi 2014-11-20 23:21:23

+0

对不起,我并不是想要添加$。更正了帖子。 – 2014-11-20 23:23:21