使用 Browserify 覆盖 Backbone 全局同步

Overide Backbone sync globally with Browserify

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 但是我无法这样做。

"globally" 是什么意思上面的代码仅替换了特定 Backbone 模型 (NotificationHeaderModel) 的同步方法。如果你想替换一切背后的实际同步,它应该是 Backbone.sync (http://backbonejs.org/#Sync)

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

    Backbone.sync = servicesNew.primaBackbone.bind(servicesNew);

更新:另一件事是您应该将方法 primaBackbone 分配给 Backbone.sync 而不是返回值。另外,重要的是你应该在你的自定义同步中同时处理案例模型和集合(如果你不需要集合,那就没问题)

更新:因为您在方法中使用了不正确的变量。单独调用 readUrl 指的是 global 范围,这显然是未定义的

完整代码

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 = this.readUrl;
            break;
        case "delete":
            options.url = this.deleteUrl+'/'+model.get("id");
            break;
        case "update":
            options.url = this.updateUrl+'/'+model.get("id");
            break;
         case "create":
            options.type = "PUT";
            options.url = this.createUrl;
            break;    
    }

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

 module.exports = servicesNew;
  1. 我会放弃 servicesNew 对象上的属性并使用 options 对象来传递 url
  2. 你的模型的同步方法不会那样工作,你正在重新分配 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/