Enlace de funciones dinámicas de JavaScript/jQuery y ajax

I'm trying to build out a dynamic list of UI elements. I can hard code the UI list now, and everything works fine. I want this to be extensible however, so I'm trying to figure a way of passing id tags and corresponding functions to be called. Essentially, the callback function for my ajax call isn't being executed, though I am posting the correct data, and receiving the correct response. Here's the code:

myModule = function () {
  var titleUI = Object.create(ChainUI());
  var memcachedId = '<?php echo $memcachedId;?>';
  return {
    printChain: function(data) {
      var territories = jQuery.parseJSON(data);
      territories.forEach(function(territory) {
    loadChain: function() {
      var url = 'traffichandler.php';
      var instruction = {'instruction': 'titleChain', 'method': 'printChain', 'memcachedId': memcachedId.toString()};
      $.post(url, instruction, this.printChain);
    loadDefault: function() {
      titleUI.loadUI(['Title Chain'], [this.loadChain]);

The corresponding titleUI code follows:

var ChainUI = function() {
 return {
 loadUI: function(uiList, funcList) {
  for(var i in uiList) {
    var toDom = '<li id="'+uiList[i]+'">';
    toDom += uiList[i];
    toDom += '</li>';
  for(var i = 0; i < uiList.length; i++) {
    var myFunc = funcList[i];
    $('#uiFunctions').delegate($('#'+uiList[i]), "click",
      function() {myFunc();}


In myModule.loadDefault, I can get this.loadChain to fire. I can't get this.printChain inside the $.post to work though. If remove all the dynamic stuff this.printChain works no problem. Don't get too hung up on the syntax, as is, the syntax is fine on my end. How can I get the ajax call back to work? Thanks!

preguntado el 22 de septiembre de 12 a las 18:09

Hmm, what does not work with the $.post? Does the ajax post fire in firebug? -

Yes, I am posting and receiving the correct data. this.printChain is not being called. I am not seeing the alert. Though the implementation of ChainUI.loadUI is not shown, I am able to pass uiList and funcList just fine. That seems to work, apparently I can't do multiple levels of function passing, I think. -

Maybe a longshot, but have you tried something like $.post(url, instruction, new myModule.printChain); -

I wouldn't dare do that. myModule is a singleton, that would break the whole design pattern. -

Que pasa si tu console.log(this); or console.log(typeof this.printChain) inside loadChain? -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.