Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use Backbone.js, but in the NO-RESTful way?

I'm now a front-end developer, and I have a project which is fine to use BackboneJS, and the server-side is written by others. Is there anyone who can tell me how to override delete, update, add, and so on in a not-RESTful way? The server-side's URL may be like this:

  • add: www.domain.com/addBookById
  • remove: www.domain.com/removeBookById

Thanks a lot!!

like image 899
Daniel.Woo Avatar asked Nov 20 '12 02:11

Daniel.Woo


People also ask

Should I use Backbone JS?

The developers should make use of Backbone JS while developing a single-page Java application. Backbone JS features Model View Framework, which allows much more than structuring JavaScript architecture. It will help the developers eliminate several issues that they might be facing while developing apps.

What is the use of backbone JS?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.

Is Backbone JS a JavaScript framework?

Backbone. js is a JavaScript framework that helps you organize your code. It is literally a backbone upon which you build your application.

Does backbone need jQuery?

You can use the Backbone. Model without jQuery, but Backbone.


1 Answers

Backbone uses Backbone.sync to manage all communication with the server. There are two important things about sync for you; first of all, it looks like this:

The method signature of Backbone.sync is sync(method, model, [options])

  • method – the CRUD method ("create", "read", "update", or "delete")
  • model – the model to be saved (or collection to be read)
  • options – success and error callbacks, and all other jQuery request options

and the second is that you can override sync on a per-model and per-collection basis. So you can add your own sync implementation to your model:

var M = Backbone.Model.extend({
    sync: function(method, model, options) {
        //...
    },
    //...
});

If you look at method you can decide which URL to use and whether you're doing a GET, POST, ... request. The model will tell you what data to send to the server. You'll want to merge options into the $.ajax options you want to use. Have a look at the standard implementation of Backbone.sync, it is pretty straight forward and should show you what you need to do: just replace the URL handling and drop some of the features you don't care about (such as emulateHTTP and emulateJSON).

like image 160
mu is too short Avatar answered Sep 20 '22 14:09

mu is too short