Scope?

20 views
Skip to first unread message

Maxus

unread,
May 10, 2012, 8:49:53 AM5/10/12
to APE Project
Can someone please help, I'm sure there is something simple I'm
missing but I just can't get it.

I'm including Clients/JavaScript.js, Demos/config.js and my custom js
script file in the parent document. Within the js script file I'm
trying to use APE but am having issues.

Within a jquery document ready function I'm loading the server etc.
ie:


var client = new APE.Client();

client.load({'channel': 'test'});
client.addEvent('load', function() {
client.core.start({'name': new Date().getTime().toString())});
});
client.addEvent('multiPipeCreate', function(pipe) {
client.onRaw('info', function(params) {
console.log('info: '+params.data.item1+' - '+params.data.item2+'
- '+params.data.item3);
});
});

This works fine. I then have a bunch of functions which I'd like to
add send events to. So I try adding this code to them:

client.addEvent('multiPipeCreate', function(pipe) {
pipe.request.send('getInfo', {'input1': 'abc', 'input2': 'xyz',
'input3': '123'});
});

Its strange as it only ever works if it's placed straight below the
top code that creates the client and connects to the server... Even if
its placed directly within an onclick listener, within the document
ready block it fails to fire. I'm sure it's scope related but can't
work it out. Please help as I'm losing the little hair I have left! :)

Thanks
Maxus

Omar Chanouha

unread,
May 10, 2012, 10:28:08 AM5/10/12
to ape-p...@googlegroups.com
Try removing the var from the client declaration. Or declare client
outside of the document.ready

-O
> --
> You received this message because you are subscribed to the Google
> Groups "APE Project" group.
> To post to this group, send email to ape-p...@googlegroups.com
> To unsubscribe from this group, send email to
> ape-project...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ape-project?hl=en
> ---
> APE Project (Ajax Push Engine)
> Official website : http://www.ape-project.org/
> Git Hub : http://github.com/APE-Project/

Maxus

unread,
May 10, 2012, 8:20:02 PM5/10/12
to APE Project
Thanks for the reply, tried both with no success. I know it's not
scope completely because if I declare 'client' globally (placing 'var
client;' at the top of doc) it knows about it. ie changing
'client.addEvent()' to 'client.add1Event' produces Uncaught
TypeError: Object [object Object] has no method 'add1Event'. But
doesn't seem to fire when its right.
Its like the object is there but they aren't on the same channel?

Maxus

unread,
May 10, 2012, 9:22:12 PM5/10/12
to APE Project
Actually cant be a channel issue as the server side module prints to
the ape log as soon as 'getinfo' is called. :/

Maxus

unread,
May 10, 2012, 10:21:45 PM5/10/12
to APE Project
Maybe I'm approaching this wrong? Here is simplified version of what
I'm trying to do.

<!DOCTYPE html PUBLIC "-//W4C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr"
lang="en">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/
jquery.min.js" type="text/javascript"></script>
<script type="text/javaScript" src="./apeDir/APE_JSF/Clients/
JavaScript.js"></script>
<script type="text/javaScript" src="./apeDir/APE_JSF/Demos/
config.js"></script>
<script>
var client;
$(document).ready(function(){
client = new APE.Client();
client.load({'channel': '*tapdicom'});
client.addEvent('load', function() {
client.core.start({'name': new
Date().getTime().toString()});
});

client.addEvent('multiPipeCreate', function(pipe) {
client.onRaw('info', function(params) {
console.log('testReturn: '+params.data.testReturn);
});
});

$('#testButton').on('click', function(){
console.log('clicky');
client.addEvent('multiPipeCreate', function(pipe) {
pipe.request.send('getInfo', {'testData': 'ABC'});
});
});
});
</script>
</head>
<body>
<div id='testButton'>click</div>
</body>
</html>

Click the button, console displays message but no event is fired
according to network. It's driving me insane! I had a bit of a hard
time getting the SQL up and running (spider monkey error), finally got
that sorted and converted everything over from my original ajax code
and it was working great from my testing page but as soon as I try to
trigger a send from elsewhere in the code it fails. :(

Louis Charette

unread,
May 10, 2012, 11:24:12 PM5/10/12
to ape-p...@googlegroups.com
Hi there,

You are adding two times the event "multiPipeCreate". That's one thing. The second thing, you don't have to be in the "addEvent" function to send a raw. The way you did it:
1) the client connect to the server. 
2) When the connection is made to the channel, the event "multiPipeCreate" is fired:  the client start listening for the "info" raw
3) When you press the testButton, the event "multiPipeCreate" is redefined and you ask to send the raw when this same event is fired again (which won't append since it already been fired. It will fire again if you join a new channel)


You can send a Raw outside of the "multiPipeCreate". Try something like this:

var client;
var myPipe;

$(document).ready(function(){
	client = new APE.Client();
	client.load({'channel': '*tapdicom'});
	client.addEvent('load', function() {
		client.core.start({'name': new Date().getTime().toString()});
	});
	
	client.addEvent('multiPipeCreate', function(pipe) {
		myPipe = pipe.getPubid();
	});
	
	client.onRaw('info', function(params) {
		console.log('testReturn: '+params.data.testReturn);
	});
$('#testButton').on('click', function(){
		console.log('clicky');
		client.core.request.send('getInfo', {'testData': 'ABC'});
	});
});


Not sure, but you can also try "client.core.getPipe(myPipe).send('getInfo', {'testData': 'ABC'});" instead of "client.core.request.send('getInfo', {'testData': 'ABC'});". 
I use my own class, so I'm not sure of the real syntax.


  - Louis

Maxus

unread,
May 11, 2012, 12:27:58 AM5/11/12
to APE Project
Thanks very much Louis, seems to be fixed with your suggestions!!! :)

For any others having similar difficulty I had to send the raw with:
client.core.getPipe(myPipe).request.send('getInfo', {'testData':
'ABC'});
Otherwise the pipe was undefined when it hit the server module.

Maxus

unread,
May 11, 2012, 10:00:58 AM5/11/12
to APE Project
Seems I've hit a road block again. Can't seem to work out how I can
add to my existing code so that when a user clicks a button they not
only receive the existing return sent to everyone, but also receive an
additional message that is only sent to them.
I thought that when the user joined the channel there is a pubid that
is created for them which I could send to the server module and then
use a command to send a raw back to this specified pubid? This is the
last thing I need to work out. I'm sorry if this has been answered a
thousand times, I promise I've searched here and on the ape site to no
avail.

Cheers

Pablo Tejada

unread,
May 11, 2012, 1:11:13 PM5/11/12
to ape-p...@googlegroups.com
No need to route the data in the server. You could could just handle
the data at the source, the client. But if you really want to send
that extra raw just write a server module that hooks the SEND command,
from there you could return the sender its raw.

Sent from my iPhone

Maxus

unread,
May 11, 2012, 10:01:14 PM5/11/12
to APE Project
Thanks Pablo, after a good nights sleep and not looking blankly
staring at the code for hours I've realised I can just use the raw
that is currently being returned to determine the origin. Thanks for
the additional information though, appreciate the help.

On May 12, 3:11 am, Pablo Tejada <ptejad...@gmail.com> wrote:
> No need to route the data in the server. You could could just handle
> the data at the source, the client. But if you really want to send
> that extra raw just write a server module that hooks the SEND command,
> from there you could return the sender its raw.
>
> Sent from my iPhone
>
Reply all
Reply to author
Forward
0 new messages