[英]Override prototype javascript object
I have the following in prototype.js: 我在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();
}
});
I am trying to add another option, but I don't want to modify the existing prototype library, but extend it so that it will have my new option. 我正在尝试添加另一个选项,但是我不想修改现有的原型库,而是对其进行扩展以使其具有我的新选项。 I have created a second js file that gets loaded after prototype.js that includes the following:
我创建了第二个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();
}
});
The alert in the override object never gets called, but it continues to use the original object. 永远不会调用覆盖对象中的警报,但它将继续使用原始对象。 I must be missing something or the Class.create is doing things dynamically after I have tried to redefine the object.
我必须丢失某些内容,否则在尝试重新定义对象之后,Class.create将动态地执行操作。
I found a way to make it work using Class#addMethods() : 我找到了一种使用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();
}
});
You should use the function http://api.prototypejs.org/ajax/Ajax/Responders/register/ 您应该使用函数http://api.prototypejs.org/ajax/Ajax/Responders/register/
Ajax.Responders.register ({
on401: function() {
window.location.href="/logout.jsp";
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.