简体   繁体   English

Backbonejs最简单的集合获取

[英]Backbonejs simplest collection fetch

I'm trying to download, parse and show a list, from the XML received from my server using Backbone.js. 我正在尝试使用Backbone.js从服务器接收的XML中下载,解析并显示列表。 The code is like: 代码如下:

var Item = Backbone.collection.extend({
    url: "http://myurl.com/file.xml",
    parse: function() {
        console.log("parse");
    },
    success: function(data) {
        console.log(data);
    },
    error: function() {
        console.log("error");
    }
});

var View1=Backbone.view.extend({
    initialize: function() {
        var item = new Item();
        item.fetch();
    }
});

When I check it in the Chrome extension, the XML file is getting downloaded but the breakpoints placed in the parse , success , error directly lands to the error . 当我在Chrome扩展程序中检查该XML文件时,正在下载XML文件,但在parsesuccesserror放置的断点将直接变为error

And it has 3 arguments, but I'm unable to extract any information from that. 它有3个参数,但我无法从中提取任何信息。

Backbone does not support fetching XML, hence, you'll need to override the sync method to provide your own custom parsing functionality. Backbone不支持获取XML,因此,您将需要覆盖sync方法以提供自己的自定义解析功能。 If you don't want to have to mess with Backbone internals, try doing your $.ajax GET first, parse your response into a proper JSON Array and then use that array with a Backbone#Collection-reset . 如果您不想弄乱Backbone内部,请尝试先执行$ .ajax GET,将响应解析为适当的JSON数组,然后将该数组与Backbone#Collection-reset结合使用

Backbone#Collection-fetch Backbone#Collection-fetch

The server handler for fetch requests should return a JSON array of models. 提取请求的服务器处理程序应返回模型的JSON数组。

Backbone#Sync 主干#同步

With the default implementation, when Backbone.sync sends up a request to save a model, its attributes will be passed, serialized as JSON, and sent in the HTTP body with content-type application/json. 使用默认实现时,当Backbone.sync发送保存模型的请求时,其属性将被传递,序列化为JSON,并在HTTP正文中使用内容类型application / json发送。 When returning a JSON response, send down the attributes of the model that have been changed by the server, and need to be updated on the client. 返回JSON响应时,请发送已由服务器更改且需要在客户端上更新的模型属性。 When responding to a "read" request from a collection (Collection#fetch), send down an array of model attribute objects. 响应来自集合(Collection#fetch)的“读取”请求时,向下发送一组模型属性对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM