Montag, Mai 21, 2007

Using webservice calls with multiple parameters

The restriction that the AJAXEngine supported only methods with one parameter had been a topic to some postings in the blog and also to some good comments with good solutions.

Because the problem was only restricted by the AJAX Engine itself and not by the underlying JavaScript proxy layer I looked for a small and neat solution and finally found that the often overlooked Javascript method apply

helps here.

The implemented way of passing more than one parameter is (like suggested) to use an Array as a return value of the prepare function. Because arrays are also supported as a parameter to the called method itself a extra hint is used to distinguish these 2 scenarios.

Here is how to implement the prepare method for this case:

prepare: function (ds) { 
  var p = new Array();
  p[0] = ajaxForms.getData(ds.form);
  p[1] = "name";
  p.multi = true; // the hint for the ajax Engine
  return(p);
},

The changes in the AJAX Engine layer is only a condition check and a call using the apply method:

// start the call
ca.call.func = ajax.Finish;
ca.call.onException = ajax.Exception;
if ((data.constructor == Array) && (data.multi != null)) // 19.05.2007
  ca.call.apply(ca, data);
else
  ca.call(data);

If you want to use this feature, you can download the ajax.js file directly. A complete version will be available some day soon including new samples and features.

Keine Kommentare: