n0minal
n0minal

Reputation: 3223

I need to override backbone.sync to allow PUT methods

I need to override Backbone.sync to allow PUT the problem is i don't know how and where to put it.

This is my Model:

define([
'underscore',
'backbone'
], function(_, Backbone) {
var Input = Backbone.Model.extend({
url: 'http://localhost/InterprisePOS/SOP/mobilecreateinvoice/',
initialize: function(){

},
toJSON : function() {
  return _.clone({ input:this.attributes });
},

});
return Input;
});

This is my Save function in my view:

save: function(){
    invoice = new Invoice();
    input = new Input();
    invoice.set({POSWorkstationID: "POS7"});
    invoice.set({POSClerkID: "admin"});
    invoice.set({CustomerName: "Alice in Wonderland Tours"});
    invoice.set({IsFreightOverwrite: true});
    invoice.set({BillToCode: "CUST-000009"});
    InvoiceCollection.add( invoice );
    //var invoices = JSON.stringify({Invoices: InvoiceCollection.toJSON()});
    _.each(this.collection.models, function(cart){
        InvoiceDetailCollection.add( cart );
    });
    input.set({ "Invoices": InvoiceCollection.toJSON() });
    input.set({ "InvoiceDetails": InvoiceDetailCollection});
    alert( JSON.stringify(input.toJSON()) );
    input.save();
}

Upvotes: 2

Views: 5496

Answers (1)

Mohamed Mansour
Mohamed Mansour

Reputation: 40149

The default Backbone sync handler maps CRUD to REST like the following:

  • create → POST /collection
  • read → GET /collection[/id]
  • update → PUT /collection/id
  • delete → DELETE /collection/id

Sometimes older servers emulate HTTP by mimicking the HTTP method with _method and X-HTTP-Method-Override header. If that is the case, you should set Backbone.emulateHTTP to true

If you want custom mappings, then you would need to override Backbone.sync. An example of overriding could be like the following:

Backbone.sync = function(method, model, options, error) {

  // Backwards compatibility with Backbone <= 0.3.3
  if (typeof options == 'function') {
    options = {
      success: options,
      error: error
    };
  }

  var resp = function(resp) {
    if (resp.status) {
      options.success(method != 'read' ? model : resp.data);
    }
    else {
      options.error('Record not found ' + resp.data);
    }
  };


  var store = model.customStorage || model.collection.customStorage;

  switch (method) {
    case 'read':    model.id ? store.read({id: model.id}, resp) : store.readAll(resp); break;
    case 'create':  store.create(model.attributes, resp); break;
    case 'update':  store.update(model.attributes, resp); break;
    case 'delete':  store.delete(model.id, resp); break;
  }
};

Where customStorage is your implementation, it could be anything you want that respects the methods I created. Some time ago, I created a backbone sync override for HTML5 WebSQL Storage, it is open sourced located on GitHub https://github.com/mohamedmansour/backbone.webStorage

I hope this helps you get started! Good Luck!

Upvotes: 7

Related Questions