4
votes

(sorry for the english)

I have a ASP .net webservice that get data from a oracle database returning JSON data.

TestWebService.asmx/getUserData

I test this using simply ajax request with jQuery

$.ajax({
     type:"POST",
     data:"{}",
     dataType:"json",
     contentType:"application/json; charset=utf-8",
     url:"TestWebService.asmx/getUserData",
     success:function(data){
         console.log(data.d);
     }
});

This work.

But now i want to try use Backbone.js

The Application have this: User data, Articles and Buy Order where a Buy order is a collection of Articles, so i think in this models for Backbone

User = Backbone.Model.extend({})
Article = Backbone.Model.extend({})
ArticleCollection = Backbone.Collection.extend({})
BuyOrder = Backbone.Model.extend({})
BuyOrderCollection = Backbone.Collection.extend({})

The Views are just 2. A Form where i show the User Data and inputs to add Articles and create the Buy Order and a Visualize view to show the Buy Orders where the user can see an check the content of one buy order clicking in the code.

The UserData, and part of the Article Data are get from the service: (User Data like name and Article Data like code, description, price, etc).

¿How can i populate the Backbone models with this data?

Thanks in advance.

1

1 Answers

6
votes

So, basically, you want to override Backbone.sync. It is the thing that is currently doing your RESTful stuff (GET/POST/PUT/DELETE) via the $.ajax function as well. See how it is implemented by default: http://documentcloud.github.com/backbone/docs/backbone.html#section-134

As you can tell, it is really quite simple... about 30 or so lines of code to map create/update/delete/read to post/put/delete/get in $.ajax.

Now that you have seen how they do it, you just implement your own using the same pattern:

Backbone.sync = function(method, model, options) {
    // your implementation
};

Once you do that, you are golden. Your models will do all the CRUD that you want them to, abstracted through your implementation of Backbone.sync.