开发者

Zend_Json_Server and dojo.rpc.JsonService, can a served class return an object?

开发者 https://www.devze.com 2023-01-18 11:16 出处:网络
I am trying to serve up my user repository via zend_json_server. The problem is the service is returning empty objects. What have i missed?

I am trying to serve up my user repository via zend_json_server. The problem is the service is returning empty objects. What have i missed? server side:

$repo = App_User_Repository::getInstance();

  $server = new Zend_Json_Server();
  $server->setClass($repo);

  if ('GET' == $_SERVER['REQUEST_METHOD']) {
      $server->setTarget('/service/json-rpc.php')
             ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
      $smd = $server->getServiceMap();

   开发者_开发百科   // Set Dojo compatibility:
      $smd->setDojoCompatible(true);
      header('Content-Type: application/json');
      echo $smd;
      return;
  }

  $server->handle();

client side:

    var object = new dojo.rpc.JsonService('/service/json-rpc.php');
    var deferred = object.getById(1);
    deferred.addBoth(function(result) {console.log(result)});

Firebug console output:

 Object {}

This should be a User object


When doing the actual RPC with the "getById()" method, an dojo.deferred object is returned. At this point, a asynchronous request is running. By using the deferred object, you can define callbacks and error handlers in advance whilst waiting for the response to be returned.

Check if the actual response object isn't empty as well. Remember, you still have to use the return keyword in your attached classes to return results back to Zend_Json_Server. Zend_Json_Server will then serialize and send back the returned value automatically. A response from Zend_Json_Server is always a serialized object in JSON, containing an id (which increments automatically with each request), an string indicating what jsonrpc version is being used (ie. 2.0) and of course a result containing the returned data from the attached class.

The setClass() method should not be a object instance, but a string containing the className of the class you want to attach. Zend_Json_Server handles the creation of the object instance by itself, as well as generating the SMD (Service Method/Mapper Description). Remember to document each public method with docblocks, as Zend_Json_Server uses those docblocks to determine the SMD.

Furthermore, it is much more handy to use a fluent-like interface with the then() method like so:

var myService = new dojo.rpc.JsonService('/service/json-rpc.php?');
var deferredObj = myService.doThis('myArgument');
deferredObj.then(callback, errorHandler).then(afterCallback).then(cleanUp);

In above example, the variables callback, errorHandler, afterCallback and cleanUp, are actually references to functions. The first then() method you call, automatically passes the rpc result to the callback function. If you throw an exception from within the attached rpc class, the errorHandler method (second optional argument of the first then() method call) will be called instead.

More information: http://www.sitepen.com/blog/2010/05/03/robust-promises-with-dojo-deferred-1-5/


I recently ran into the same problem and it wasn't a problem with dojo deferred. I'm assuming getById(1) is your remote function call, in which case if your server finds results dojo shouldn't get the empty object. Even using addBoth method on a deferred object would still show the result from the server, which leads me to believe your problem is not in any of the code you've listed, but getById(1) call in your App_User_Repository class. Did you use Zend_Dojo_Data or something else to json encode before returning? That would clobber your result, Zend_Json_Server does the encoding for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消