我在prototype.js中有以下内容:
Ajax.Base = Class.create({
initialize: function(options) {
alert('in original');
this.options = {
method: 'post',
asynchronous: true,
contentType: 'application/x-www-form-urlencoded',
encoding: 'UTF-8',
parameters: '',
evalJSON: true,
evalJS: true
};
Object.extend(this.options, options || { });
this.options.method = this.options.method.toLowerCase();
if (Object.isHash(this.options.parameters))
this.options.parameters = this.options.parameters.toObject();
}
});
我正在尝试添加另一个选项,但我不想修改现有的原型库,而是扩展它以便它有我的新选项。我创建了第二个js文件,它在prototype.js之后加载,包括以下内容:
Ajax.Base = Class.create({
initialize: function(options) {
alert('in override');
this.options = {
method: 'post',
asynchronous: true,
contentType: 'application/x-www-form-urlencoded',
encoding: 'UTF-8',
parameters: '',
evalJSON: true,
evalJS: true,
on401: function() { window.location.href="/logout.jsp"; }
};
Object.extend(this.options, options || { });
this.options.method = this.options.method.toLowerCase();
if (Object.isHash(this.options.parameters))
this.options.parameters = this.options.parameters.toObject();
}
});
覆盖对象中的警报永远不会被调用,但它会继续使用原始对象。在尝试重新定义对象后,我必须遗漏某些内容或者Class.create正在动态地进行操作。
答案 0 :(得分:0)
我找到了一种方法,可以使用Class#addMethods():
Ajax.Base.addMethods({
initialize: function(options) {
this.options = {
method: 'post',
asynchronous: true,
contentType: 'application/x-www-form-urlencoded',
encoding: 'UTF-8',
parameters: '',
evalJSON: true,
evalJS: true,
on401: function() { window.location.href="/logout.jsp"; }
};
Object.extend(this.options, options || { });
this.options.method = this.options.method.toLowerCase();
if (Object.isHash(this.options.parameters))
this.options.parameters = this.options.parameters.toObject();
}
});
答案 1 :(得分:0)
您应该使用函数http://api.prototypejs.org/ajax/Ajax/Responders/register/
Ajax.Responders.register ({
on401: function() {
window.location.href="/logout.jsp";
})