Question

J'ai refactoré le code de travail pour utiliser les objets appropriés et je ne parviens plus à faire fonctionner AJAX.Request de Prototype. Le code est ci-dessous, cela fonctionne dans le contexte de la DataTable de YUI:

SearchTable.prototype.setTableColumns = function (transport) {
      this.dataTableColumns = transport.responseText.evalJSON();
      this.dataTableCallback();
};

SearchTable.prototype.setTableConfiguration = function (transport) {
  this.dataTableConfiguration = transport.responseText.evalJSON();
  this.dataTableCallback();
};

SearchTable.prototype.show = function () {
  ....
  new Ajax.Request(this.dataProxy, {
    method: 'get',
    parameters: {
      format: 'json',
      param: 'columns'
    },
    onSuccess: this.setTableColumns
  });

 new Ajax.Request(this.dataProxy, {
   method: 'get',
   parameters: {
     format: 'json',
     param: 'configuration'
   },
   onSuccess: this.setTableConfiguration
  });
}
};

 SearchTable.prototype.dataTableCallback = function () {
        ....
 }

Mon problème est que dataTableCallback n'est jamais appelé. Apparemment, il lance une exception selon laquelle this est indéfini, ce que je peux comprendre: les callbacks ne sont pas appelés dans un contexte d'objet et donc ce n'est jamais attribué. J'ai essayé de faire du curry, mais j'ai échoué.

La question qui se pose est la suivante: comment puis-je réussir à faire fonctionner ce projet?

Était-ce utile?

La solution

Créer une fermeture pour "ceci":

SearchTable.prototype.show = function () {
  ....

  var self = this;

  new Ajax.Request(this.dataProxy, {
    method: 'get',
    parameters: {
      format: 'json',
      param: 'columns'
    },
    onSuccess: function(transport) { self.setTableColumns(transport); }
  });

Autres conseils

Essayez ceci:

onSuccess: this.setTableColumns.bind(this)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top