javascript - 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 override          // default construction of 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 model 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 trying override backbone.sync method backbone globally unable so.

  1. i ditch attributes on servicesnew object , use options object pass urls
  2. your models' sync methods won't work that, you're reassigning backbone.sync , don't pass argument.

a potential solution 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 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 demo http://jsfiddle.net/mn0eo6eb/


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -