backbone - no return after calling fetch success c

2019-09-08 08:43发布

问题:

newbie in backbonejs here. was wondering if anyone can help me with the same issue. i tried the solution above and still no luck. i am having the same issue as this post Backbone.js - Getting JSON back from url.

here is my code

  // 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); 
  ¦ }                                                                    
  });                                         

回答1:

Thanks to @Wracker the solution to this was I was calling an XML data in return and the ajax call can't figure out the output format.. the fix to this is

By default the fileType is automatically detected but perhaps in this case it's failing at figuring out the correct format. Therefore I'd try defining the fileType explicitly to xml/xthml/script...

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

But there is also one catch, you have to parse to output into a typical javascript object. (jQuery.parseXML() may come in handy in such case)