// 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);
}
});
在视图中
:last-of-type
我试图全局覆盖Backbone.sync方法Backbone但是我无法这样做。
答案 0 :(得分:1)
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);
}
});
答案 1 :(得分:0)
全球"你是什么意思?以上代码仅替换该特定Backbone Model(NotificationHeaderModel)的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);
更新:另一件事是你应该将方法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;