Hi,
Ah, with more context it's clear what's going wrong. You're making a
common -- and understandable -- mistake about how closures work:
> document.observe('dom:loaded', function(){
> var x1, elm;
> x1='proyecto';
> elm = $(x1);
> if (elm) {
> elm.observe('change', function(evt) {
> document.fire('sic:'+x1);
> });
> }
>
> x1='material';
> elm = $(x1);
> if (elm) {
> elm.observe('change', function(evt) {
> document.fire('sic:'+x1);
> });
> }
>
> });
There you've defined two functions, attaching one of them to the
change event of the 'proyecto' element and the other to 'material'.
But! The functions do _exactly the same thing_. Both of the
functions inherits a _reference_ to x1, *not* its value. So both will
use the last value you assign to x1 -- namely, 'material'. Even the
first one.
I've written up a couple of things about closures that may be helpful,
and specifically that walk you through this business of references vs.
values:
http://blog.niftysnippets.org/2008/02/closures-are-not-complicated.html
http://blog.niftysnippets.org/2008/03/closures-by-example.html
HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available
On Jun 27, 6:47 pm, "Miguel Beltran R." <[email protected]> wrote:
> 2009/6/27 T.J. Crowder <[email protected]>
>
>
>
> > Hi,
>
> > Your two examples are not identical. In the one using 'proyecto'
> > literally, your if statement will always be true because you're not
> > using $, you're using
>
> > if('proyecto')
>
> > where to be identical to the x1 version you would need
>
> > if($('proyecto'))
>
> Sorry another mistake in the mail, but again the real function is fine :P
>
>
>
>
>
> > I don't know why that would make any difference since presumably the
> > element does exist (otherwise the literal example wouldn't work), but
> > it's a difference.
>
> > I'd suggest you take this code:
>
> > 001: document.observe('dom:loaded', function(){
> > 002: var x1, elm;
> > 003: x1='proyecto';
> > 004:
> > 005: elm = $(x1);
> > 006: if (elm) {
> > 007: elm.observe('change', function(evt) {
> > 008: document.fire('sic:'+x1);
> > 009: });
> > 010: }
> > 011: });
>
> > And use a debugger like Firebug or similar to put a breakpoint on line
> > 6 and another one on line 8, and see what you see. Is 'elm' undefined
> > on line 6? Does document.fire on line 8 get called when the change
> > happens?
>
> Using firebug show me what elm is the correct element, but...
>
> This are my custom events
>
> document.observe('sic:proyecto',function(evt){
> var s = evt.memo.campo_llenar || '';console.log(s);
> var a = new Ajax.Updater('estado', 'carga_datos.htm', {
> evalScripts: true,
> parameters: {control: 'datos_proyecto',
> ejercicio_id:$F('ejercicio_id')
> ,campo_llenar: s
> ,proyecto: $F('proyecto'+s)}
> });
> a=null;
>
> });
>
> document.observe('sic:material',function(evt){
> var s = evt.memo.campo_llenar || '';console.log(s);
> var a = new Ajax.Updater('estado', 'carga_datos.htm', {
> evalScripts: true,
> parameters: {control: 'datos_material',
> ejercicio_id: $F('ejercicio_id')
> ,campo_llenar: s
> ,material: $F('material'+s)}
> });
> a=null;
>
> });
>
> first put only one observe (proyecto) and work fine, later add the second
> observe (material), here is where the thing go crazy
> Using firebug I see what when the event "change" is called by "proyecto"
> this call 'sic:material' ¿uh?
>
> document.observe('dom:loaded', function(){
> var x1, elm;
> x1='proyecto';
> elm = $(x1);
> if (elm) {
> elm.observe('change', function(evt) {
> document.fire('sic:'+x1);
> });
> }
>
> x1='material';
> elm = $(x1);
> if (elm) {
> elm.observe('change', function(evt) {
> document.fire('sic:'+x1);
> });
> }
>
> });
>
> if change to this, work fine
> var x2;
> x2='material';
> elm = $(x2);
> if (elm) {
> elm.observe('change', function(evt) {
> document.fire('sic:'+x2);
> });
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---