Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #13166 > unrolled thread

question on scope in callback

Started byJedrin <jrubiando@gmail.com>
First post2012-03-31 05:01 -0700
Last post2012-04-06 21:46 +0100
Articles 5 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  question on scope in callback Jedrin <jrubiando@gmail.com> - 2012-03-31 05:01 -0700
    Re: question on scope in callback David Mark <dmark.cinsoft@gmail.com> - 2012-03-31 14:26 -0700
      Re: question on scope in callback Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-04-05 01:25 +0200
        Re: question on scope in callback Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-05 06:11 +0200
        Re: question on scope in callback Dr J R Stockton <reply1214@merlyn.demon.co.uk.not.invalid> - 2012-04-06 21:46 +0100

#13166 — question on scope in callback

FromJedrin <jrubiando@gmail.com>
Date2012-03-31 05:01 -0700
Subjectquestion on scope in callback
Message-ID<b991d3b3-27f0-4815-9e24-1fcf65e8c23e@do4g2000vbb.googlegroups.com>
I seem to be having a problem with this code below that uses ruby/faye
websockets. I am not sure
what stuff is in scope when the callback in the subscribe() function.
I have stuff like the marker_arr variable here, but I am not sure how
I can see that in the callback ? I've done a fair amount of
javascript, but not for a long time.  What I am trying to do is move
pins around on an openlayers map, the call comes into the callback
from the server/websocket which has the info of where I want to move
previously created pins. If I can't access any javascript variables
outside the callback, I am not sure how to do what I want to do. I am
not sure if that is the case or not though.

 This is a simplified example I created from my code and not the
actual code.


<body onload="init();">

<script>
  var marker_arr;

   function init() {

    map = new OpenLayers.Map("basicMap");
    marker_arr = new Array();

     var client = new Faye.Client('http://localhost:9292/faye');
     var public_subscription = client.subscribe('/messages/public',
function(data) {
         var jobj = eval('(' + data.json + ')');
         // this work ??????
         marker = marker_arr[jobj.id];
         });


       marker_arr[1] = OpenLayers.Marker(lonLat);

    });

</script>

[toc] | [next] | [standalone]


#13171

FromDavid Mark <dmark.cinsoft@gmail.com>
Date2012-03-31 14:26 -0700
Message-ID<aeb672f0-9a41-4e74-aadc-9763dbbea520@2g2000yqk.googlegroups.com>
In reply to#13166
On Mar 31, 8:01 am, Jedrin <jrubia...@gmail.com> wrote:
> I seem to be having a problem with this code below that uses ruby/faye
> websockets.
> I am not sure
> what stuff is in scope when the callback in the subscribe() function.

That's indeed a problem.

> I have stuff like the marker_arr variable here, but I am not sure how
> I can see that in the callback ?

Because it is declared globally.

> I've done a fair amount of
> javascript, but not for a long time.  What I am trying to do is move
> pins around on an openlayers map, the call comes into the callback
> from the server/websocket which has the info of where I want to move
> previously created pins. If I can't access any javascript variables
> outside the callback, I am not sure how to do what I want to do. I am
> not sure if that is the case or not though.

Globals are global.

>
>  This is a simplified example I created from my code and not the
> actual code.
>
> <body onload="init();">
>
> <script>

You should use the TYPE attribute for all SCRIPT elements.  Never mind
what HTML5 says as many browsers in use today predate that
"standard".  They claimed that leaving it out worked in "all
browsers", but their research was predictably deficient (really hard
to check every case).

>   var marker_arr;
>
>    function init() {
>
>     map = new OpenLayers.Map("basicMap");
>     marker_arr = new Array();
>
>      var client = new Faye.Client('http://localhost:9292/faye');
>      var public_subscription = client.subscribe('/messages/public',
> function(data) {
>          var jobj = eval('(' + data.json + ')');
>          // this work ??????

You tell me. :)

[toc] | [prev] | [next] | [standalone]


#13226

FromEric Bednarz <bednarz@fahr-zur-hoelle.org>
Date2012-04-05 01:25 +0200
Message-ID<m2r4w3gjvp.fsf@nntp.bednarz.nl>
In reply to#13171
David Mark <dmark.cinsoft@gmail.com> writes:

> You should use the TYPE attribute for all SCRIPT elements.

With what value?

> Never mind
> what HTML5 says as many browsers in use today predate that
> "standard".

I rather wouldn't mind what HTML 4 says, namely

<http://www.w3.org/TR/html4/interact/scripts.html#adef-type-SCRIPT>
(e.g., "text/javascript")
 ^^^^

where the exempli gratia predates RFC 4329 more than half a decade. 

[toc] | [prev] | [next] | [standalone]


#13228

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-04-05 06:11 +0200
Message-ID<3523734.CVebrN057Y@PointedEars.de>
In reply to#13226
Eric Bednarz wrote:

> David Mark <dmark.cinsoft@gmail.com> writes:
>> You should use the TYPE attribute for all SCRIPT elements.
> 
> With what value?

"text/javascript" appears to be supported best in (X)HTML:

<http://web.archive.org/web/20100904140711/http://pointedears.de/scripts/test/mime-
types/> ¹)


PointedEars
___________
¹  For some reason the original vanished from my website.  The resource will
   be restored after having been updated.
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

[toc] | [prev] | [next] | [standalone]


#13272

FromDr J R Stockton <reply1214@merlyn.demon.co.uk.not.invalid>
Date2012-04-06 21:46 +0100
Message-ID<4UxhsqMCZ1fPFwJg@invalid.uk.co.demon.merlyn.invalid>
In reply to#13226
In comp.lang.javascript message <m2r4w3gjvp.fsf@nntp.bednarz.nl>, Thu, 5
Apr 2012 01:25:14, Eric Bednarz <bednarz@fahr-zur-hoelle.org> posted:

>David Mark <dmark.cinsoft@gmail.com> writes:
>
>> You should use the TYPE attribute for all SCRIPT elements.
>
>With what value?

Using "text/vbscript" works well, in the right circumstances.
ISTR seeing at least a third value (possibly text/perl) commended.

-- 
 (c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web