2017-04-12 39 views
0

je suit dans prototype.js:prototype Override objet javascript

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(); 
    } 
}); 

Je suis en train d'ajouter une autre option, mais je ne veux pas modifier la bibliothèque prototype existant, mais étends de façon à aura ma nouvelle option. J'ai créé un second fichier js qui est chargé après prototype.js qui comprend les éléments suivants:

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(); 
    } 
}); 

L'alerte dans l'objet de remplacement ne fait jamais appelé, mais il continue d'utiliser l'objet original. Je dois manquer quelque chose ou le Class.create fait des choses dynamiquement après que j'ai essayé de redéfinir l'objet.

Répondre

0

je trouve un moyen de le faire fonctionner à l'aide 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(); 
    } 
});