简体   繁体   English

使用Browserify全局覆盖骨干同步

[英]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;

My Model: 我的模特:

    // 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;

IN View: 在视图中:

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

I am trying to override the Backbone.sync method Backbone globally however i am unable to do so. 我正在尝试全局覆盖Backbone.sync方法Backbone,但是我无法这样做。

  1. I would ditch attributes on the servicesNew object and use the options object to pass the urls 我会抛弃servicesNew对象上的属性,并使用options对象来传递网址
  2. your models' sync methods won't work like that, you're reassigning Backbone.sync and you don't pass any argument. 您模型的同步方法将无法正常工作,您需要重新分配Backbone.sync并且不传递任何参数。

A potential solution could be 一个潜在的解决方案可能是

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

And the model definition 以及模型定义

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

});

And a demo http://jsfiddle.net/mn0eo6eb/ 还有一个演示http://jsfiddle.net/mn0eo6eb/

What do you mean by "globally" the above code only replaces the sync method for that particular Backbone Model (NotificationHeaderModel). 您所说的“全局”是什么意思,以上代码仅替换了特定骨干模型(NotificationHeaderModel)的sync方法。 If you want to replace the actual sync behind everything, it should be Backbone.sync ( http://backbonejs.org/#Sync ) 如果要替换所有内容后面的实际同步,则应为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);

Update: another thing is that you should assign the method primaBackbone to Backbone.sync instead of the returned value. 更新:另一件事是您应该将primaBackbone方法primaBackboneBackbone.sync而不是返回值。 Also, it is important that you should handle both case model and collection in your custom sync (if you don't need collections, then it is ok) 另外,重要的是您应该在自定义同步中处理案例模型和集合(如果不需要集合,那就可以了)

Update: Because you are using incorrect variables in the method. 更新:因为您在方法中使用了不正确的变量。 Calling readUrl alone refers to the global scope which is obviously undefined 单独调用readUrl是指显然未定义的global范围

Full code 完整代码

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM