骨干将覆盖与Browserify全球同步 [英] Overide Backbone sync globally with Browserify

查看:210
本文介绍了骨干将覆盖与Browserify全球同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  VAR servicesNew = {
    readUrl:,
    deleteUrl:,
    器updateURL:,
    createUrl:,    primaBackbone:函数(方法,模型,期权){
        选择|| (选项= {});        VAR beforeSend = options.beforeSend;
        options.beforeSend =功能(XHR){          xhr.setRequestHeader('授权','承载52b20db1-4bcb-426E-9bbf-a53a826249f3')
          如果(beforeSend)返回beforeSend.apply(这一点,参数);
        };
        //传递options.url将覆盖
        //在Backbone.sync URL的默认构造        开关(方法){
            案读:
                options.url = readUrl;
                打破;
            案删除:
                options.url = deleteUrl +'/'+ model.get(ID);
                打破;
            案更新:
                options.url = +器updateURL'/'+ model.get(ID);
                打破;
             案创造:
                options.type =PUT;
                options.url = createUrl;
                打破;
        }        如果(options.url)
            返回Backbone.sync.call(模型,方法,模型,期权);
    }
}module.exports = servicesNew;

我的型号:

  //文件名:型号/项目
VAR骨干=要求(骨干),
    网址=要求('../../库/网址),
    servicesNew =要求('../../库/ servicesnew');VAR NotificationHeaderModel = Backbone.Model.extend({      同步:功能(){        servicesNew.readUrl = Urls.notifications.unread;
        servicesNew.createUrl = Urls.notifications.list;
        servicesNew.deleteUrl = Urls.notifications.list;
        servicesNew.updateUrl = Urls.notifications.list;        返回Backbone.sync = servicesNew.primaBackbone();      }
});
//返回模型模块
module.exports = NotificationHeaderModel;

在View:

  this.model.fetch({
   成功:函数(模型,响应,期权){    的console.log(响应);
     _this.template = notificationTemplate;     。_this $ el.html(_this.template({notificationData:响应,notificationType:notifyMsg.notificationType()
      ,notificationMessage:notifyMsg.notificationMessage()}));
   },
   错误:功能(模型,XHR,期权){
    警报(xhr.result.Errors);
  }
});

我试图在全球范围内覆盖Backbone.sync方法主干但是我不能这样做。


解决方案

  1. 我会沟 servicesNew 对象的属性和使用选项对象来传递网址

  2. 您模型的同步方法是行不通的那样,你重新分配 Backbone.sync ,你不传递任何参数。

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

  VAR servicesNew = {
    primaBackbone:函数(方法,模型,期权){
        选择|| (选项= {});        VAR beforeSend = options.beforeSend;
        options.beforeSend =功能(XHR){
            xhr.setRequestHeader('授权','承载52b20db1-4bcb-426E-9bbf-a53a826249f3')
            如果(beforeSend)返回beforeSend.apply(这一点,参数);
        };        开关(方法){
            案读:
                options.url = options.readUrl;
                打破;
            案删除:
                options.url = options.deleteUrl +'/'+ model.get(ID);
                打破;
            案更新:
                options.url = options.updateUrl +'/'+ model.get(ID);
                打破;
             案创造:
                options.type =PUT;
                options.url = options.createUrl;
                打破;
        }        如果(options.url)
            返回Backbone.sync.call(模型,方法,模型,期权);
    }
}

和模型定义

  VAR NotificationHeaderModel = Backbone.Model.extend({      同步:函数(方法,模型,期权){
          选项​​= _.defaults({},期权,{
              readUrl:Urls.notifications.unread,
              createUrl:Urls.notifications.list,
              deleteUrl:Urls.notifications.list,
              器updateURL:Urls.notifications.list
          });          返回servicesNew.primaBackbone.call(模型,方法,模型,期权);
      }});

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

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.

解决方案

  1. I would ditch attributes on the servicesNew object and use the options object to pass the urls
  2. your models' sync methods won't work like that, you're reassigning Backbone.sync and you don't pass any argument.

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/

这篇关于骨干将覆盖与Browserify全球同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆