Great work, more examples!

183 views
Skip to first unread message

alx...@googlemail.com

unread,
Sep 5, 2008, 1:28:37 AM9/5/08
to Cappuccino & Objective-J
I just wanted to say what fantastic work I think this is. I've been
intrigued by all of this since hearing about 280slides on the Ajaxian
podcast a few month ago.

The code examples in the PhotoDemo and Puzzle are useful. I think a
useful addition would be a very simple example demonstrating the use
of server communication/RPC/JSON etc.

Something really simple like loading table with 10 rows of data would
be a good starting point for me, personally.

I'll post something if I get there before one of the experts does.

Alex

Eisen Montalvo

unread,
Sep 5, 2008, 2:15:45 AM9/5/08
to objec...@googlegroups.com
I just finished a quick test to connect to a backend using JSON to get
the data and POST to send the data back. Is is basically a hack, but
will give you the idea.

To connect to a php backend script:
var url = baseUrl + "jsonScript.php";
var request = [CPURLRequest requestWithURL: url];
var data = [CPURLConnection sendSynchronousRequest: request
returningResponse:nil error:nil];
var str = [data description];

'str' will contain a JSON object or array that you will need to
decode. I hope somebody adds a method to CPData to convert a JSON
object to a CPArray or CPDictionary.

To send data back to a php backend script using POST:
var url = baseUrl + "postScript.php";
var request = [CPURLRequest requestWithURL: url];
var body = "field1=" + field1Value + "&field2=" + field2Value +
"&field3=" + field3Value;

[request setHTTPMethod: "POST"];
[request setValue:"application/x-www-form-urlencoded"
forHTTPHeaderField:"Content-Type"];
[request setValue:[body length] forHTTPHeaderField:"Content-Length"];

[request setHTTPBody: body];

var data = [CPURLConnection sendSynchronousRequest: request
returningResponse:nil error:nil];

'data' contains any results sent back from the script that you can use
to validate the results of the operation.

This are synchronous connections. I haven't tried with asynchronous
yet, but I'll be playing with them soon. I'm pretty sure there are
better ways to handle the connection to the backend, I'm not an
expert. I just wanted to share some code that might help you.

Eisen

Kevin Hoffman

unread,
Sep 5, 2008, 6:18:48 AM9/5/08
to objec...@googlegroups.com
There's a JSON connector already written for us in the Flickr demo
source code. Might make some of this easier.

Eisen Montalvo

unread,
Sep 5, 2008, 9:13:06 AM9/5/08
to objec...@googlegroups.com
Thank you Kevin. On my first glance at the demos I didn't notice the
link to the source code. Would've saved some time! :-) Anyway it was
fun. I love working with Cappuccino.

Eisen

Sent from my iPhone 3G

alx...@googlemail.com

unread,
Sep 5, 2008, 6:44:49 AM9/5/08
to Cappuccino & Objective-J, Alex Reid
Yes, I found CPJSONPConnection.j. Have modified the Hello World
example to request the text from a server.
The one thing that caught me out was having to wrap the returned JSON
as named by the jsoncallback GET parameter, i.e. $_GET['jsoncallback']
( {...} ).

-(void)connection:(CPJSONPConnection)aConnection didReceiveData:
(CPString)data
{
[label setStringValue:"Received: "+data.greeting];
}

-(void)connection:(CPJSONPConnection)aConnection didFailWithError:
(CPString)error
{
alert(error);
}

-(void)swap:(id)sender {
var request = [CPURLRequest requestWithURL:"hello.php"];
var connection = [CPJSONPConnection sendRequest: request callback:
"jsoncallback" delegate: self];
}

(Apologies if this appears twice, not sure where my posts are
going...)

alx...@googlemail.com

unread,
Sep 5, 2008, 6:34:48 AM9/5/08
to Cappuccino & Objective-J
Yeah, I discovered CPJSONPConnection.j as used in the photo demo.

The below code is based on the Hello world example. It assumes a JSON
string is returned by hello.php that looks like the following:

{ "greeting":"Hello from request."}

You need to wrap the JSON as named by the jsoncallback GET parameter.
i.e.

<?=$_GET['jsoncallback']?>( { "greeting":"Hello from request."} )

AppController is passed as a delegate to handle the response when it
arrives, so these two methods are needed.

-(void)connection:(CPJSONPConnection)aConnection didReceiveData:
(CPString)data
{
[label setStringValue:"Received: "+data.greeting];
}

-(void)connection:(CPJSONPConnection)aConnection didFailWithError:
(CPString)error
{
alert(error);
}

-(void)swap:(id)sender {
var request = [CPURLRequest requestWithURL:"hello.cfm"];
var connection = [CPJSONPConnection sendRequest: request callback:
"jsoncallback" delegate: self];
}

Hope this helps if anyone was unsure, as I was!

Alex

alx...@googlemail.com

unread,
Sep 5, 2008, 3:53:51 AM9/5/08
to Cappuccino & Objective-J
Thanks, that's helpful. I have since found that the photo demo and
CPJSONPConnection.j answers a lot of my questions regarding
asynchronous communication.

Tom Robinson

unread,
Sep 5, 2008, 2:38:02 PM9/5/08
to objec...@googlegroups.com
Actually CPJSONPConnection is meant for JSONP APIs like Flickr (JSONP
is a method used to circumvent the browser's same origin policy)

You should use CPURLConnection (a thin wrapper around XMLHttpRequest)
and parse the result using CPJSObjectCreateFromJSON() (essentially
"eval" with checks to make sure the input is valid JSON)

You won't need to do the JSONP style wrapping with this method.

(btw the reason your posts didn't show up is posts by new members need
to be approved. We should turn that off though)

Sent from my iPhone

On Sep 5, 2008, at 5:44 AM, "alx...@googlemail.com" <alx...@googlemail.com

alx...@googlemail.com

unread,
Sep 5, 2008, 3:36:23 PM9/5/08
to Cappuccino & Objective-J
Ahh, that clears that up.

Thanks. I think this info would make a good wiki page as it's the kind
of basic thing people want to do after getting 'Hello World!'
working.

On Sep 5, 7:38 pm, Tom Robinson <t...@280north.com> wrote:
> Actually CPJSONPConnection is meant for JSONP APIs like Flickr (JSONP  
> is a method used to circumvent the browser's same origin policy)
>
> You should use CPURLConnection (a thin wrapper around XMLHttpRequest)  
> and parse the result using CPJSObjectCreateFromJSON() (essentially  
> "eval" with checks to make sure the input is valid JSON)
>
> You won't need to do the JSONP style wrapping with this method.
>
> (btw the reason your posts didn't show up is posts by new members need  
> to be approved. We should turn that off though)
>
> Sent from my iPhone
>
> On Sep 5, 2008, at 5:44 AM, "alxs...@googlemail.com" <alxs...@googlemail.com
Reply all
Reply to author
Forward
0 new messages