English 中文(简体)
XML 响应的ackbone.js 后bone.js 后bone.js - 分析错误前未调用的解析函数
原标题:Backbone.js with XML response - parse function not called before parse error

我使用Backbone.js来构建一个应用程序,通常通过JSON与网络服务进行沟通。

一个网络服务会在成功或 XML 出错时返回 JSON 。 我需要解析 XML 响应以确定错误, 但 Backbone 的 JSON 中心思维正在给我造成问题 。

我有一个包含分析函数的收藏。 当服务返回 JSON 时, 分析函数总是被调用, 在这种情况下, 我只需要返回响应对象 。 但是, 当服务返回 XML 我的抓取调错误回回调功能被调用, 并用 arguments [1] parseerrror 传递错误对象。 进一步的挖掘显示有一个意外的 lt; 字符 。

为什么我的剖析功能在被抛出牧羊人之前没有被叫来分析XML?此外,为什么在成功的JSON电话中,它通过了JavaScript 对象(表明JSON字符串已经被解析了)? 剖析功能难道不应该做剖析吗?

以下相关守则、任何建议都十分赞赏。

var myCollection = Backbone.Collection.extend({

initialize : function() {
    ...
},

fetch: function(options) {

    var options = {data: {...}, error: this.onFetchError};

    Backbone.Collection.prototype.fetch.call(this, options);
},

onFetchError: function(arg1, arg2, arg3) {

    debugger
},

parse: function(response) {

    debugger
    if(typeof response ===  object ) {
        return response;
    }
}
});

return myCollection;
问题回答

文档对此模糊不清。 Model.fetch([ options]) 使用 Backbone.sync 用于同步的docs 表示您可以使用“所有其他jQuery请求选项”,意思是“a href=”“http://api.jquery.com/jQuery.ajaax/”支持的任何东西。

因此,为了将回应作为原始文本,你可以做到:

thiss. fetch ({ dataType: text}); ({ dataType: text}); ({ dataType: text});

然后,在您 parse 函数中,您可以任意处理回复。

侧边注意:我在试图获取 XML 模型时发现了这一点, 并且用该数据类型测试 。

如果您查看默认抓取 :

fetch: function(options) {
  options = options ? _.clone(options) : {};
  var model = this;
  var success = options.success;
  options.success = function(resp, status, xhr) {

    //-->parse only invoked on success     
    if (!model.set(model.parse(resp, xhr), options)) return false;
    if (success) success(model, resp);
  };

  //-->parse not invoked on error
  options.error = Backbone.wrapError(options.error, model, options);
  return (this.sync || Backbone.sync).call(this,  read , this, options);
}

您可以看到, 分析永远不会被错误条件调用。 但是, 包装错误( 如下表) 将会给您最初的回复 :

Backbone.wrapError = function(onError, originalModel, options) {
  return function(model, resp) {
    resp = model === originalModel ? resp : model;
    if (onError) {
      onError(originalModel, resp, options);
    } else {
      originalModel.trigger( error , originalModel, resp, options);
    }
  };
}; 

所以,你的阿尔格2 将会得到回应, 并且可以想象, 你可以把它传递到你的 剖析功能上。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签