Changeset 8428 for lang/javascript

Show
Ignore:
Timestamp:
03/27/08 07:56:48 (5 years ago)
Author:
stomita
Message:

lang/javascript/jsonptester : added support for static callback (like google visualization api)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/jsonptester/trunk/js/jsonptester.js

    r6722 r8428  
    4040          fieldLabel : 'Append No Cache Param', 
    4141          checked : true 
     42        }, { 
     43          xtype : 'checkbox', 
     44          name : 'staticCallback',  
     45          fieldLabel : 'Use Static Callback', 
     46          checked : false 
     47        }, { 
     48          xtype : 'textfield', 
     49          name : 'callbackFuncName',  
     50          width : 200, 
     51          disabled : true, 
     52          fieldLabel : 'Callback Function' 
    4253        }] 
    4354      }], 
     
    6879  }); 
    6980 
     81  // toggling static callback and dynamic parameter passing callback 
     82  var staticCallbackCheck = win.find('name', 'staticCallback')[0]; 
     83  var callbackParamField = win.find('name', 'callbackParam')[0]; 
     84  var callbackFuncNameField = win.find('name', 'callbackFuncName')[0]; 
     85  staticCallbackCheck.on('check', function(checkbox, flag) { 
     86    callbackParamField[ flag ? "disable" : "enable" ](); 
     87    callbackFuncNameField[ flag ? "enable" : "disable" ](); 
     88  }); 
     89 
    7090  win.show(); 
    7191 
     
    7595  // format  
    7696  //    #<callbackParam>:<jsonpServiceURL> 
     97  //    or  
     98  //    #[<callbackFuncName>]:<jsonServiceURL> 
    7799  var m = location.href.match(/#(.*):(.*)$/); 
    78   if (m) form.setValues({ callbackParam : m[1], url : decodeURIComponent(m[2]) }); 
     100  if (m) { 
     101    var options = {}; 
     102    options.url = decodeURIComponent(m[2]); 
     103    var m2 = m[1].match(/^\[(.*)\]$/); 
     104    if (m2) { 
     105      options.staticCallback = true; 
     106      options.callbackFuncName = m2[1]; 
     107    } else { 
     108      options.callbackParam = m[1]; 
     109    } 
     110    form.setValues(options); 
     111  } 
    79112 
    80113 
     
    98131        url : values.url, 
    99132        callbackParam : values.callbackParam == '' ? 'callback' : values.callbackParam, 
     133        staticCallback : values.staticCallback == 'on', 
     134        callbackFuncName : values.callbackFuncName, 
    100135        timeout : + values.timeout, // force cast string to number 
    101136        nocache : values.nocache == 'on' 
     
    179214  doAsyncLoad : function(node, callback) { 
    180215    if (!node.attributes.value) { 
     216      function onSuccess(o) {  
     217        node.attributes.value = o; 
     218        callback.onSuccess(describeProperties(o)); 
     219      } 
     220       
     221      if (node.attributes.staticCallback && node.attributes.callbackFuncName.length>0) { 
     222        // creating success handler function placement variable and its namespaces 
     223        var funcnames = node.attributes.callbackFuncName.split('.');   
     224        (function(o) { 
     225          var name = funcnames.shift(); 
     226          if (funcnames.length==0) { 
     227            o[name] = onSuccess; 
     228          } else { 
     229            o[name] = o[name] || {}; 
     230            arguments.callee(o[name]); 
     231          } 
     232        })(window); 
     233      } 
     234 
    181235      // using jQuery JSONP call 
     236      // In case of static calback, success handler doesn't callbacked from here.  
    182237      $.ajax({ 
    183238        url : node.attributes.url, 
     
    185240        jsonp : node.attributes.callbackParam, 
    186241        cache : !node.attributes.nocache, 
    187         success : function(o) {  
    188           node.attributes.value = o; 
    189           callback.onSuccess(describeProperties(o)); 
    190         }, 
     242        success : onSuccess, 
    191243        error : callback.onFailure 
    192244      }); 
     245 
    193246    } else { 
    194247      callback.onSuccess(describeProperties(node.attributes.value))