var servicesNew = {
    readUrl :"",
    deleteUrl :"",
    updateUrl :"",
    createUrl :"",

    primaBackbone : function(method, model, options) {
        options || (options = {});

        var beforeSend = options.beforeSend;
        options.beforeSend = function(xhr) {

          xhr.setRequestHeader('Authorization','Bearer 52b20db1-4bcb-426e-9bbf-a53a826249f3')
          if (beforeSend) return beforeSend.apply(this, arguments);
        };
        // passing options.url will override
        // the default construction of the url in Backbone.sync

        switch (method) {
            case "read":
                options.url = readUrl;
                break;
            case "delete":
                options.url = deleteUrl+'/'+model.get("id");
                break;
            case "update":
                options.url = updateUrl+'/'+model.get("id");
                break;
             case "create":
                options.type = "PUT";
                options.url = createUrl;
                break;
        }

        if (options.url)
            return Backbone.sync.call(model, method, model, options);
    }
}

module.exports = servicesNew;


我的模特:

    // Filename: models/project
var Backbone = require('backbone'),
    Urls= require('../../libs/urls'),
    servicesNew = require('../../libs/servicesnew');

var NotificationHeaderModel = Backbone.Model.extend({

      sync: function(){

        servicesNew.readUrl = Urls.notifications.unread;
        servicesNew.createUrl = Urls.notifications.list;
        servicesNew.deleteUrl = Urls.notifications.list;
        servicesNew.updateUrl = Urls.notifications.list;



        return Backbone.sync = servicesNew.primaBackbone();

      }


});
// Return the model for the module
module.exports = NotificationHeaderModel;


在视图中:

    this.model.fetch({
   success: function(model, response, options){

    console.log(response);
     _this.template = notificationTemplate;

     _this.$el.html(_this.template({notificationData: response,notificationType:notifyMsg.notificationType()
      ,notificationMessage:notifyMsg.notificationMessage()}));
   },
   error: function(model, xhr, options){


    alert(xhr.result.Errors);
  }
});


我正在尝试全局覆盖Backbone.sync方法Backbone,但是我无法这样做。

最佳答案

我会抛弃servicesNew对象上的属性,并使用options对象传递网址
您模型的同步方法将无法正常工作,您正在重新分配Backbone.sync,并且不传递任何参数。


一个潜在的解决方案可能是

var servicesNew = {
    primaBackbone : function(method, model, options) {
        options || (options = {});

        var beforeSend = options.beforeSend;
        options.beforeSend = function(xhr) {
            xhr.setRequestHeader('Authorization','Bearer 52b20db1-4bcb-426e-9bbf-a53a826249f3')
            if (beforeSend) return beforeSend.apply(this, arguments);
        };

        switch (method) {
            case "read":
                options.url = options.readUrl;
                break;
            case "delete":
                options.url = options.deleteUrl+'/'+model.get("id");
                break;
            case "update":
                options.url = options.updateUrl+'/'+model.get("id");
                break;
             case "create":
                options.type = "PUT";
                options.url = options.createUrl;
                break;
        }

        if (options.url)
            return Backbone.sync.call(model, method, model, options);
    }
}


以及模型定义

var NotificationHeaderModel = Backbone.Model.extend({

      sync: function(method, model, options){
          options = _.defaults({}, options, {
              readUrl: Urls.notifications.unread,
              createUrl: Urls.notifications.list,
              deleteUrl: Urls.notifications.list,
              updateUrl: Urls.notifications.list
          });

          return servicesNew.primaBackbone.call(model, method, model, options);
      }

});


和演示http://jsfiddle.net/mn0eo6eb/

关于javascript - 使用Browserify全局覆盖主干同步,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30230661/

10-10 00:11