views:

72

answers:

2

I asked a fairly poor question yesterday, but I still have the same general problem. I wanted to add a variable property to an object, and the solution I attempted was to do an ajax request to receive the variable, and then set the property to the response. I think I have tried this at least 100 different ways:

Ext.onReady(go);

function go() {

  var x; 

  function getRating(id, callback) {
      Ext.Ajax.request({
          url: '/index.php/ajax/do_rate',
          method: 'POST',
          success: function(result)
          {
              jsonData = Ext.util.JSON.decode(result.responseText);
              x = jsonData.result;
          }
      });
  }
  r2 = new Ext.ux.form.Rater({
      displayValue: x,
      maxValue: 10
  });

  var simple = new Ext.FormPanel({
    items: [r2]
  });
  simple.render(document.body);
}

If it try it as above, x is undefined. If I try to create r2 prior to the ajax request, and set its displayValue within the request, the correct displayValue is shown for the object in the console, but it does not render with the correct displayValue. I don't think it makes it in time. Thanks for the help, I'm thinking I might need a 'callback', but cannot figure out the proper method.

The working code is below:

Ext.onReady(go);

function go() {
    Ext.Ajax.request({
        url: '/index.php/ajax/do_rate',
        method: 'POST',
        success: function(result)
        {
            jsonData = Ext.util.JSON.decode(result.responseText);
            var r2 = new Ext.ux.form.Rater({
                displayValue: jsonData.result,
                maxValue: 10
            });
            var simple = new Ext.FormPanel({
            items: [r2]
            });
            simple.render(document.body);
        }
    });

}

+1  A: 

Just do everything you need to do with the data inside the "success" callback.

function go() {

  function getRating(id, callback) {
      Ext.Ajax.request({
          url: '/index.php/ajax/do_rate',
          method: 'POST',
          success: function(result)
          {
              jsonData = Ext.util.JSON.decode(result.responseText);

              var x = jsonData.result;

              var r2 = new Ext.ux.form.Rater({
                  displayValue: x,
                  maxValue: 10
              });

              var simple = new Ext.FormPanel({
                items: [r2]
              });
              simple.render(document.body);
          }
      });
  }
}
Pointy
solved, thanks. modified code added above, in case this can help someone else.
Orbit
ah, i see you had that on the way. thanks again. Spent way to many hours of my life working this out. Just out of curiosity (possibly need), is there any way to pop a response out (perhaps a wait until request is complete option)?
Orbit
I'm not sure I get what you mean. You can't really "wait" in Javascript (in a browser). It's an event-driven model, so all you can do is tell the system that you want to do something when an event happens. That's what the "success" function is: a callback that's invoked when the HTTP request completes. That completion is an event, just like a mouse click is an event, as far as the browser is concerned.
Pointy
Thanks pointy. So I guess the problem is everything below the request fires off very rapidly, and the request takes a few ms to complete. I see why the value was undefined. I guess my problem is that this bit is just a tiny component on the page. I don't know what the lead dev would think if I put the entire page contents and loader inside this tiny request. I really have to update or refresh the component somehow inside the success handler, but I'm not sure how to do this either.
Orbit
Well you can spread the code out however you want; you can always put stuff in a *separate* function,, or dozens of separate functions, and just make sure that you call that code from the handler. The call to the Ajax request will return *immediately* after initiating the HTTP transaction, so you really don't have much choice other than to defer other processing. But all the code doesn't have to be inside the "success" handler. It's the control flow that matters.
Pointy
A: 

you need to put this code:

r2 = new Ext.ux.form.Rater({
    displayValue: x,
    maxValue: 10
});

var simple = new Ext.FormPanel({
    items: [r2]
});

inside your AJAX POST CALLBACK like this:

success: function(result)
        {
            jsonData = Ext.util.JSON.decode(result.responseText);
            x = jsonData.result;
            r2 = new Ext.ux.form.Rater({
                displayValue: x,
                maxValue: 10
            });

            var simple = new Ext.FormPanel({
                items: [r2]
            });
        }
CuSS