Hi, I am having a problem with the way RJS generates code. Say I have html like this: <div id="parent"> <div id="child1"></div> <div id="child2"></div> </div> and RJS code like this: parent = page["parent"] parent.down(''#child1'').highlight parent.down(''#child2'').highlight the generated JS code becomes: $("1").down("#a").highlight().down("#b").highlight(); which is incorrect, because the second down() starts from within #child1, whereas it should have started from within #parent. By not declaring the "parent" variable and use page["parent"] for every highlight() call it would work, but it''s not very DRY. I could also append a bunch of up() and down() in a long chain, but the code gets messy difficult to follow. I was wondering if there are any good ways to generate code that are correct, clean, and DRY? Thanks in advance! Bob
Hi Bob, On Fri, 2009-09-04 at 18:53 -0700, bob wrote:> I am having a problem with the way RJS generates code. Say I have > html like this: > > <div id="parent"> > <div id="child1"></div> > <div id="child2"></div> > </div> > > and RJS code like this: > > parent = page["parent"] > parent.down(''#child1'').highlight > parent.down(''#child2'').highlight > > the generated JS code becomes: > > $("1").down("#a").highlight().down("#b").highlight(); > > which is incorrect, because the second down() starts from within > #child1, whereas it should have started from within #parent. > > By not declaring the "parent" variableI think maybe the problem you''re having results from declaring a variable, and perhaps naming a DOM element, ''parent''. parent is a Javascript property; as in: window.parent Try using a different name and see if the problem persists. HTH, Bill> and use page["parent"] for > every highlight() call it would work, but it''s not very DRY. I could > also append a bunch of up() and down() in a long chain, but the code > gets messy difficult to follow. > > I was wondering if there are any good ways to generate code that are > correct, clean, and DRY? > > Thanks in advance! > > Bob > >
Oh, actually it''s just a simplified example code to focus on the concept. The problem is that when I use a ruby variable containing an element multiple times, RJS chains them together while disregarding the traversal methods, which generates incorrect code. On Sep 5, 10:30 am, bill walton <bwalton...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Bob, > > > > On Fri, 2009-09-04 at 18:53 -0700, bob wrote: > > I am having a problem with the way RJS generates code. Say I have > > html like this: > > > <div id="parent"> > > <div id="child1"></div> > > <div id="child2"></div> > > </div> > > > and RJS code like this: > > > parent = page["parent"] > > parent.down(''#child1'').highlight > > parent.down(''#child2'').highlight > > > the generated JS code becomes: > > > $("1").down("#a").highlight().down("#b").highlight(); > > > which is incorrect, because the second down() starts from within > > #child1, whereas it should have started from within #parent. > > > By not declaring the "parent" variable > > I think maybe the problem you''re having results from declaring a > variable, and perhaps naming a DOM element, ''parent''. parent is a > Javascript property; as in: > > window.parent > > Try using a different name and see if the problem persists. > > HTH, > Bill > > > and use page["parent"] for > > every highlight() call it would work, but it''s not very DRY. I could > > also append a bunch of up() and down() in a long chain, but the code > > gets messy difficult to follow. > > > I was wondering if there are any good ways to generate code that are > > correct, clean, and DRY? > > > Thanks in advance! > > > Bob