Skip to content Skip to sidebar Skip to footer

Backbone.js Collection Call Xml File Using This.fetch Error

myBook = Backbone.Collection.extend({ initialize: function(models, geturl) { var self = this; this.url = geturl; this.fetch({ dataType: 'xml

Solution 1:

Backbone expects the server response as JSON. You should override Model.parse with a function that parses the XML and returns a plain Javascript object. jQuery.parseXML is pretty handy for mapping simple XML to an object.

Assuming your XML response would look like this:

<root><id>1</id><foobar="foobar">foo</foo></root>

And you wanted the model properties to look like:

{
  id:1,
  foo:'foo',
  foobar:'foobar'
}

You could parse it as follows:

var YourModel = Backbone.Model.extend({
  parse: function(xml) {
    var$xml = $.parseXML(xml);
    return {
      id:      parseInt($xml.find('id').text(), 10),
      foo:     $xml.find('foo').text(),
      foobar:  $xml.find('foo').attr('bar')
    }
  }  
});

Post a Comment for "Backbone.js Collection Call Xml File Using This.fetch Error"