BackboneJS 与 XML ajax

发布于 2024-12-19 22:40:39 字数 1759 浏览 1 评论 0原文

这是 JS 新手提出的由两部分组成的问题。

因此,我尝试按照 Thomas Davis 的教程 使用 requireJS 创建一个主干应用程序。

  1. 如何通过对提供 XML 数据的服务器的 ajax 调用来创建 Backbone 集合? collections.fetch() 似乎需要 JSON 后端。

  2. 在尝试一些事情时,我最终得到了以下代码,其中从 Ajax 成功回调中填充集合“bookStore”时页面不会刷新。

    这是我到目前为止所取得的进展。

    var bookListView = Backbone.View.extend({
        el: $("#books"),
        初始化:函数(){
            这个视图=这个;
            $.ajax({
                类型:“获取”,
                url: "books.xml",
                数据类型:“xml”,
                成功:函数(数据){
                    控制台.log(数据);
                    $(data).find('book').each(function (index) {
                        var bookTitle = $(this).find('name').text();
                        bookStore.add({ 标题: bookTitle });
    
                        控制台.log(seid);
                    });
                    thisView.collection = bookStore;
                    thisView.collection.bind('add', thisView.tryBind);
    
                }
            }).done(函数(消息){
                Alert("检索到的数据:" + msg);
            });
    
            this.collection = bookStore;
            this.collection.bind("add", this.exampleBind);
            this.collection.bind("刷新", function () { thisView.render(); });
            /*
            // 这个有效!
            bookStore.add({ name: "book1" });
            bookStore.add({ name: "book2" });
            bookStore.add({ name: "book3" });
            */
        },
        tryBind:函数(模型){
            控制台.log(模型);
        },
        渲染:函数(){
            变量数据 = {
                书籍:this.collection.models,
            };
            var generatedTemplate = _.template(bookListTemplate, 数据);
            $("#books").html(compiledTemplate);
        }
    });
    

在这里,“初始化”函数中的成功回调似乎正在正确处理数据并添加到集合中。但是,页面没有刷新。

当我单步执行 Firebug 控制台时,页面会刷新。我该如何解决这个问题?

this is a two part question from a JS newbie.

So, I was trying to create a backbone application using requireJS by following Thomas Davis's tutorial.

  1. How do I go create Backbone collections out of an ajax call to a server that provides data in XML? collections.fetch() seem to be expecting a JSON backend.

  2. while trying some things, I ended up with the following code, in which the page doesn't refresh upon populating the collection "bookStore" from within Ajax success-callback.

    Here is how far I have gotten so far.

    var bookListView = Backbone.View.extend({
        el: $("#books"),
        initialize: function () {
            thisView = this;
            $.ajax({
                type: "GET",
                url: "books.xml",
                dataType: "xml",
                success: function (data) {
                    console.log(data);
                    $(data).find('book').each(function (index) {
                        var bookTitle = $(this).find('name').text();
                        bookStore.add({ title: bookTitle });
    
                        console.log(seid);
                    });
                    thisView.collection = bookStore;
                    thisView.collection.bind('add', thisView.tryBind);
    
                }
            }).done(function (msg) {
                alert("Data retrieved: " + msg);
            });
    
            this.collection = bookStore;
            this.collection.bind("add", this.exampleBind);
            this.collection.bind("refresh", function () { thisView.render(); });
            /*
            // This one works!
            bookStore.add({ name: "book1" });
            bookStore.add({ name: "book2" });
            bookStore.add({ name: "book3" });
            */
        },
        tryBind: function (model) {
            console.log(model);
        },
        render: function () {
            var data = {
                books: this.collection.models,
            };
            var compiledTemplate = _.template(bookListTemplate, data);
            $("#books").html(compiledTemplate);
        }
    });
    

Here, the success call-back in the "initialize" function seems to be processing the data properly and adding to the collection. However, the page doesn't refreshed.

While I was stepping through the Firebug console, the page gets refreshed however. How do I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎无痕… 2024-12-26 22:40:39
  1. 您可以覆盖默认的parse函数以提供XML支持。它应该返回转换为 JSON 的数据 http://backbonejs.org/#Collection-parse< /p>

  2. < p>将渲染绑定到 reset 事件,而不是 Backbone<1.0 的 refresh 或绑定到 sync 事件Backbone>=1.0

它可能看起来像这样

var Book = Backbone.Model.extend();

var Books = Backbone.Collection.extend({
    model: Book,
    url: "books.xml",

    parse: function (data) {
        var $xml = $(data);

        return $xml.find('book').map(function () {
            var bookTitle = $(this).find('name').text();
            return {title: bookTitle};
        }).get();
    },

    fetch: function (options) {
        options = options || {};
        options.dataType = "xml";
        return Backbone.Collection.prototype.fetch.call(this, options);
    }
});

var bookListView = Backbone.View.extend({
    initialize: function () {
        this.listenTo(this.collection, "sync", this.render);
    },

    render: function () {
        console.log(this.collection.toJSON());
    }
});

var bks = new Books();
new bookListView({collection: bks});
bks.fetch();

还有一个演示 http://jsfiddle.net/ULK7q/73/

  1. You can override the default parse function to provide XML support. It should return the data transformed into JSON http://backbonejs.org/#Collection-parse

  2. Bind the render to a reset event instead of refresh for Backbone<1.0 or to a sync event for Backbone>=1.0

It could look like this

var Book = Backbone.Model.extend();

var Books = Backbone.Collection.extend({
    model: Book,
    url: "books.xml",

    parse: function (data) {
        var $xml = $(data);

        return $xml.find('book').map(function () {
            var bookTitle = $(this).find('name').text();
            return {title: bookTitle};
        }).get();
    },

    fetch: function (options) {
        options = options || {};
        options.dataType = "xml";
        return Backbone.Collection.prototype.fetch.call(this, options);
    }
});

var bookListView = Backbone.View.extend({
    initialize: function () {
        this.listenTo(this.collection, "sync", this.render);
    },

    render: function () {
        console.log(this.collection.toJSON());
    }
});

var bks = new Books();
new bookListView({collection: bks});
bks.fetch();

And a demo http://jsfiddle.net/ULK7q/73/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文