Is it possible to replace the proxy code generated by Jayrock to
utilise prototype code.
Cheers
C
<add name="proxy"
type="Jayrock.Json.Rpc.Web.JsonRpcProxyGenerator, Jayrock" />
Alternatively, you can make your own proxy a new feature by giving it
its own name, as in:
<add name="myproxy"
type="MyProxy, MyAssembly" />
The <script> tag can then just reference it like this:
<script
type="text/javascript"
src="http://localhost/myservice.ashx?myproxy">
Does this help?
- Atif
I'm afraid I'm still in the dark.
I've located the file that generates the JavaScript proxy code
(JsonRpcProxyGenerator.cs). Do I rewrite this file? or do I write an
alternative?
Do I need to write alternatives to proxy functions: callSync,
callAsync, setupHeaders, etc.
Can I be really cheeky and ask to see your code for implementing this.
Thanks again
C
Ideally, you'd write an alternative. Jayrock is designed with an open
design so that you can replace the various components with your own
implementation if the base doesn't suit. If you re-write the file,
you'll have trouble syncing up with a future version. So drop Jayrock
into your project and just write a new alternative that is registered to
respond to the "proxy" feature.
>>
Do I need to write alternatives to proxy functions: callSync,
callAsync, setupHeaders, etc.
<<
I'd imagine so. I don't know Prototype so I wouldn't be able to tell you
exactly what you'd have to do. But basically, those *are* the three
methods through which all calls in the proxy tunnel.
Let me know if I can clear things up further.
-----Original Message-----
From: jay...@googlegroups.com [mailto:jay...@googlegroups.com] On
Behalf Of <oo</
Next I copied JsonRpcProxyGenerator.cs and renamed it to
PrototypeProxyGenerator.cs changing the class name:
public sealed class PrototypeProxyGenerator : JsonRpcServiceFeature
The only changes to the javascript I've made for the moment is:
alert('Hello Proto');
Then I recompiled - no errors - so far so good
I then added:
<add name="protoProxy"
type="Jayrock.Json.Rpc.Web.PrototypeProxyGenerator,
Jayrock" />
to my web.config
and changed the querystring on my handler: Handler.ashx?protoProxy
but I get the following error:
Could not load type 'Jayrock.Json.Rpc.Web.PrototypeProxyGenerator' from
assembly 'Jayrock'.
Exception Details: System.TypeLoadException: Could not load type
'Jayrock.Json.Rpc.Web.PrototypeProxyGenerator' from assembly 'Jayrock'.
Am I on the correct path?
Any ideas?
C
You got pretty much everything right except one piece, I suspect. When
you created PrototypeProxyGenerator, did you place it in your own
project or did you put it back into Jayrock? If PrototypeProxyGenerator
is sitting in your own project, then the assembly name would be the
culprit. In other words, the ??? below would have to be replaced with
your main project's assembly name.
<add name="protoProxy"
type="Jayrock.Json.Rpc.Web.PrototypeProxyGenerator, ???" />
- Atif
-----Original Message-----
From: jay...@googlegroups.com [mailto:jay...@googlegroups.com] On
Behalf Of <oo</
Sent: Thursday, August 17, 2006 9:07 PM
To: Jayrock
Subject: Re: Replace proxy code
So the namespace - Jayrock - should be OK
The error is being created by:
\Jayrock\Json\Rpc\Web\JsonRpcWebGateway.cs Line: 134
Type featureType = Type.GetType(featureTypeSpec, true);
Do I need to register my new class (PrototypeProxyGenerator) anywhere
in the JROCK project?
Can you think of any reason why
"Jayrock.Json.Rpc.Web.PrototypeProxyGenerator" is not being recognised?
Thanks for all the help so far
C
Doh
That's all I need to do now is find out what code I need to replace.
You mention in another post that you can use a v2 proxy that does not
include RPC code so you can use your own client library. Would this be
easier that rewriting the proxy class.
Would you be so kind as to explain how this works.
Thanks again.
C
function yuiAsyncRequest(url, request, callback)
{
return YAHOO.util.Connect.asyncRequest("POST", url,
{
success : function(o)
{
if (typeof(callback) !== "function")
return;
callback(JSON.eval(o.responseText));
},
failure : function(o)
{
if (typeof(callback) !== "function")
return;
callback({ xmlHTTP : o });
}
},
"JSON-RPC=" + encodeURIComponent(JSON.stringify(request)));
}
function yuiChannel(call)
{
return yuiAsyncRequest(call.url, call.request, call.callback);
}
Q1)
Is this code to generated using: Handler.ashx?newProxy
or did you use: Handler.ashx?Proxy&v=2 and use the Yahoo code on the
client side?
Q2)
The prototype syntax is:
window.onload = function() {
var url = 'http://mydomain.com/ReturnMeSomething.aspx';
var params = '';
var ajax = new Ajax.Updater
(
'ReturnDiv',
url,
{
method: 'get',
parameters: params,
onFailure: reportError
}
);
}
I'm used to doing remote calls where "ReturnMeSomething.aspx" will
return the HTML for the client script (myDiv.innerHTML, etc).
The problem I have is, I don't know what url the code snippet (above)
is referencing, hence I don't know what I should use in place of
"ReturnMeSomething.aspx" in my example.
If you could shed any light I would greatly appreciate it
C
I used "?proxy&v=2" and then the Yahoo code on the client side.
Answer to Q2:
OK, I had a bit of look into Prototype tonight and I came up with the
following rough channel implementation that (very similar to the version
for Yahoo, in fact) you can use with the v2 proxy in Jayrock:
function protoChannel(call)
{
return new Ajax.Request(
call.url, {
postBody : "JSON-RPC=" +
encodeURIComponent(JSON.stringify(call.request)),
onSuccess : function (xhr) {
var response = JSON.eval(xhr.responseText);
if (response.error != null)
window.status = response.error.message;
else
call.callback(response.result);
}
});
}
Next, drop in the scripts:
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="Demo.ashx?proxy&v=2"></script>
Here, I'm using the demo service that comes with Jayrock. I'm assuming
that whatever HTML file you create for testing this is in the same path
as Demo.ashx. Copy Protoype.js there too.
Now you can call the methods of the demo service like this:
DemoService.rpc.now(alert).call(protoChannel);
DemoService.rpc.format("Price = {0:c}", 123,
alert).call(protoChannel);
Note that the standard built-in alert method here is being used as the
callback so when the results come back, they just pop-up in the browser.
Needless to say, you can tailor the protoChannel function to your liking
and custom or global error-handling strategies. I've just provided a
simple rudimentary implementation here that doesn't do a lot of error
checking.
In short, here's what's going on. The methods on v2 proxy never actually
make calls anymore. All they do is just capture the method and
parameters and generate a call object and return that. This call object
itself has a call method that takes a channel as its sole parameter.
When you invoke the call method and pass in the channel, the call object
turns around and calls the channel, passing itself as the first and only
parameter. From there on, the channel really does the hard work of,
well, channeling the call. Jayrock is no longer involved until the call
reaches the server. The channel has all of the essential properties
available from the call object, like the URL of the service, the request
contents and the callback function, at its disposal. When a response is
available from the server, the channel then turns around and calls the
callback function associated with the call and that's really it. The
benefit you have now is that you can switch your client-side Ajax
framework from one day to the next and Jayrock couldn't care less as
long as you provide a channel for whatever framework du jour you happen
to be using. You can sense some evidence of this from the fact that
we've already considered two channels, namely Yahoo and Prototype.
Hope this helps.
Enjoy,
Atif
-----Original Message-----
From: jay...@googlegroups.com [mailto:jay...@googlegroups.com] On
Behalf Of <oo</
Sent: Friday, August 18, 2006 6:31 PM
To: Jayrock
Subject: Re: Replace proxy code
I have your example working :-)
Note: The standard built-in alert method only works in IE!
You also mention that your using the alert as callback. What else can
I use for the callback? I'm having difficulty writing the response to
an .innerHTML
If I use the code :
var myTest = DemoService.rpc.now().call(protoChannel);
document.getElementById('divReturn').innerHTML = myTest;
I get:
[object Object]
If I - alert(xhr.responseText); (in the protoChannel)
Ir shows a JSON text - {"id":"1","result":"2006-08-22..."}
So I tried alert(myTest["result"]());
But I get:
myTime.result is not a function
Also, (when I finally get it working) I would like to write a tutorial
on using prototype with JROCK. Is it OK with you if I use info from
this discussion?
Cheers
C
DemoService.rpc.now(myCallback).call(protoChannel);
function myCallback(result) {
$('divReturn').innerHTML = result;
}
If you want to get fancy, you can provide the callback as an anonymous
function:
DemoService.rpc.now(
function(result) {
$('divReturn').innerHTML = result
}).call(protoChannel);
For sake of brevity, I'm using the $ shorthand from Prototype to do
document.getElementById so the net effect is the same.
-----Original Message-----
From: jay...@googlegroups.com [mailto:jay...@googlegroups.com] On
Behalf Of <oo</
Absolutely! In fact, I'd be grateful if you could write up a tutorial.
Where do you intend on publishing it?
BTW, I am currently reviewing some of my design choices in order to make
things simpler. Would you be willing to update your tutorial with some
minor changes when a newer release comes out?
I've approached http://www.4guysfromrolla.com and am awaiting their
reply.
> BTW, I am currently reviewing some of my design choices in order to make
> things simpler. Would you be willing to update your tutorial with some
> minor changes when a newer release comes out?
I can't see that being a problem.
Fantastic! I'm looking forward writing this up.
C
I'm now up and running!
Here's my working test code:
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="Demo.ashx?proxy&v=2"></script>
<script type="text/javascript">
/* <![CDATA[ */
function protoChannel(call)
{
return new Ajax.Request(
call.url, {
postBody : "JSON-RPC=" +
encodeURIComponent(JSON.stringify(call.request)),
onSuccess : function (xhr) {
var response = JSON.eval(xhr.responseText);
//alert(xhr.responseText);
if (response.error != null)
window.status = response.error.message;
else
call.callback(response.result);
}
});
}
var objTest = {
init : function() {
// set local object vars here
this.run();
},
run : function() {
// run bulk of behavior here
DemoService.rpc.now(this.fillDiv).call(protoChannel);
},
fillDiv : function(result) {
// Setup callback function
$('divReturn').innerHTML = result;
}
}
function initializer() {
objTest.init();
}
Event.observe(window, 'load', initializer, false);
/* ]]> */
</script>
<div style="border: solid thin grey; margin: 10px; padding: 10px;"
id="divReturn"></div>
Atif your the Man!
A sincere thanks for all your help, it's been very much appreciated.
C
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="Demo.ashx?proxy&v=2"></script>
<div id="UpdateMe"></div>
<script type="text/javascript" language="javascript">
function remoteInnerHTMLbyID(call)
{
return new Ajax.Updater(
"UpdateMe",
call.url, {
method: 'get',
}
);
}
DemoService.rpc.now().call(remoteInnerHTMLbyID);
</script>
I didn't expext that!
Sorry, but you didn't expect what?
-----Original Message-----
From: jay...@googlegroups.com [mailto:jay...@googlegroups.com] On
Behalf Of <oo/<
Sent: Wednesday, August 23, 2006 12:09 PM
To: Jayrock
Subject: [Jayrock] Re: Replace proxy code
When I run the code it brings back the DemoService help page:
This is JSON-RPC service that demonstrates the basic features of the
Jayrock library.
The following JSON-RPC methods are supported (try these using the
DemoService test page): ....................
I gather this is because I'm doing a 'get' not a 'post'!
If I change it to post I revice a JROCK error:
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: s
Source Error:
Line 75: {
Line 76: string request = Request.Form.Count == 1 ?
Request.Form[0] : Request.Form["JSON-RPC"];
Line 77: return new StringReader(request);
Line 78: }
Line 79: else
It's not important, I was trying to simplify the code by using
Ajax.Updater rather than
Ajax.Request. Ajax.Updater handles the job of updating the contents of
a element based on its ID.
Ajax.Updater(
"divToUpdate",
call.url, {
I was simply amused by the result!
C
-----Original Message-----
From: jay...@googlegroups.com [mailto:jay...@googlegroups.com] On
Behalf Of <oo/<
Sent: Wednesday, August 23, 2006 1:35 PM
To: Jayrock
Subject: [Jayrock] Re: Replace proxy code