backbone - 调用获取成功回调后没有 return

backbone - no return after calling fetch success callback

这里是 backbonejs 的新手。想知道是否有人可以帮助我解决同样的问题。我尝试了上面的解决方案,但仍然没有运气。我遇到了与此相同的问题 post Backbone.js - Getting JSON back from url.

这是我的代码

  // Model for News Releases
  var NewsItem = Backbone.Model.extend({
  ¦ defaults: {
  ¦ ¦ releaseID: 'no releaseID at the moment',
  ¦ ¦ date: 'no date at the moment',
  ¦ ¦ title: 'no title at the moment'
  ¦ }
  });

  // Collection for our Model
  var NewsItems = Backbone.Collection.extend({
  ¦ model: NewsItem,
  ¦ url: url + "/thewheelsonthebus.php",                                 
  ¦ parse: function(response) {                                          
  ¦ ¦ console.log(response);                                             
  ¦ ¦ return response;                                                   
  ¦ }                                                                    
  });                                                                    

  var e = new NewsItems();                                               
  e.fetch({                                                              
  ¦ success: function(collection, response) {                            
  ¦ ¦ console.log('this should return something on console' + response); 
  ¦ }                                                                    
  });                                         

感谢@Wracker,解决这个问题的方法是我在 return 中调用 XML 数据,而 ajax 调用无法确定输出格式..修复这是

默认情况下会自动检测文件类型,但在这种情况下可能无法确定正确的格式。因此,我会尝试将文件类型明确定义为 xml/xthml/script...

this.fetch({ 
  dataType: "xml", 
  async: true, 
  success: function(collection, xml) { .... }
});

但也有一个问题,您必须解析输出为典型的 javascript 对象。 (jQuery.parseXML() 在这种情况下可能会派上用场)