Pages

Monday, July 16, 2012

How to simulate AJAX requests in JSFiddle

  1. set the URL as /echo/json/  [you need that last slash]
  2. if you are doing a read action (such as in Model.load) you need to set the proxy.actionMethods.read to "POST"
  3. set "json" as an extraParam and pass the json string that you want echoed back
  4. you can optionally set a delay in extraParams

Here is a little function to help you simulate an AJAX request for a proxy:
function sim(proxy, data, delay){

  if(typeof data !== 'string'){
    data = Ext.JSON.encode(data);
  }

  proxy.url = '/echo/json/';
  proxy.actionMethods.read = "POST";
  proxy.extraParams.json = data;
  proxy.extraParams.delay= typeof delay == 'undefined' ? 0 : delay;
}

//usage:

sim(MyModel.proxy, {name:"neil"}, 2);

MyModel.load(1, {
  callback: function(record, operation){ console.log( record.get('name'); ) }
});

Example:

//jsfiddle.net/el_chief/9ksWE/



/* this is a function to simulate ajax requests in jsfiddle

This is the Sencha Touch 2.01 compatible version
 */
function sim(proxy, data, delay) {
    if (typeof data !== 'string') {
        data = Ext.JSON.encode(data);
    }

    proxy.setUrl('/echo/json/');
    proxy.setExtraParams({
        json:data,
        delay: typeof delay === 'undefined' ? 0 : delay
    });
    proxy.setActionMethods({read:'POST'});
}

No comments:

Post a Comment