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.
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.
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?