最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I need to override backbone.sync to allow PUT methods - Stack Overflow

programmeradmin9浏览0评论

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();
}

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();
}
Share Improve this question asked Mar 29, 2012 at 2:45 n0minaln0minal 3,2259 gold badges50 silver badges72 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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 patibility 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./mohamedmansour/backbone.webStorage

I hope this helps you get started! Good Luck!

发布评论

评论列表(0)

  1. 暂无评论