Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:41:26+00:00 2026-05-27T09:41:26+00:00

this is a two part question from a JS newbie. So, I was trying

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:41:27+00:00Added an answer on May 27, 2026 at 9:41 am
    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a two part question from a WPF animation newbie. First, here is
This is a two-part question. I'm using jQuery for a project and wanting to
This is a 2 part question. Question 1: I am trying to create a
Two part question Part one: I am trying to create an ASynchronous request to
Two part question. I am trying to download multiple archived Cory Doctorow podcasts from
This is a two part question. A dumb technical query and a broader query
This is a two part question. Is it possible to take advantage of xVal
This is a two-part question: First, I am interested to know what the best
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is kinda....a two part question. The first one is much more important than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.