Bill Walton
2006-Apr-10 15:40 UTC
[Rails] RJS newbie - need help with iterating / existence test
Greetings! I''m new to Ajax and am trying to get a basic app working as a vehicle for learning it. The app is an extension of the cookbook tutorial. My intent is to be able to add a list of ingredients for a recipe. The page to add ingredients uses Ajax. As ingredients are added to the page they''re displayed in a "green-bar" format like that used in the Depot tutorial. Ingredients are added to the page using form_remote_tag to update a <div> that contains 3 <span>s: one for the ingredient name, one for quantity, and one that contains a link to delete the ingredient. The delete link uses link_to_remote which in turn uses an RJS view to remove the item from the page. The stage I''m at now is that, when an item is removed from the page, it messes up the green-bar effect. So I need to iterate through the <div>s, changing their CSS class. I''ve got the change to class working (thanks to help from Ed Frederick) on a single item with: page << "$(item3).className = ''Listline1'';" (In figuring this out, I''m entering 3 items, deleting the 2nd, then changing the class of item3 which is now 2nd in the list) So now I to figure out how to iterate through the remaining items, either by starting at the top or working from the deletion point down. In either case, I need to anticipate that this might not be the first / only deletion and that there will be gaps in the item numbering sequence. So I need to test for an item''s existence before I pipe the line above (with the appropriate substitutions for ListlineY) into the page. Questions... To iterate through the list, will I be using Ruby, Javascript, or what? Is there a simple existence test? I''m hoping for something like if item#{x}.exists? Am I on the right track here? Thanks in advance for any assistance / advice. Advice on a good book to put me on track would be especially appreciated! Best regards, Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060410/f45edb21/attachment-0001.html
Edward Frederick
2006-Apr-10 16:09 UTC
[Rails] RJS newbie - need help with iterating / existence test
Hi Bill, Been meaning to write you back on the previous emails, no time yet--soon, hopefully. In short, you can just use Ruby in your RJS template for iteration. Generic Example: @items.each do |item| page << "$(#{item.id}).className = ''#{(some condition on item) ? ''class1'' : ''class2''}'';" end Cheers, Ed -- Ed Frederick - edwardfrederick.com On 4/10/06, Bill Walton <bill.walton@charter.net> wrote:> > Greetings! > > I''m new to Ajax and am trying to get a basic app working as a vehicle for > learning it. The app is an extension of the cookbook tutorial. My intent > is to be able to add a list of ingredients for a recipe. The page to add > ingredients uses Ajax. As ingredients are added to the page they''re > displayed in a "green-bar" format like that used in the Depot tutorial. > > Ingredients are added to the page using form_remote_tag to update a <div> > that contains 3 <span>s: one for the ingredient name, one for quantity, and > one that contains a link to delete the ingredient. The delete link uses > link_to_remote which in turn uses an RJS view to remove the item from the > page. > > The stage I''m at now is that, when an item is removed from the page, it > messes up the green-bar effect. So I need to iterate through the <div>s, > changing their CSS class. > > I''ve got the change to class working (thanks to help from Ed Frederick) on a > single item with: > > page << "$(item3).className = ''Listline1'';" > > (In figuring this out, I''m entering 3 items, deleting the 2nd, then changing > the class of item3 which is now 2nd in the list) > > So now I to figure out how to iterate through the remaining items, either by > starting at the top or working from the deletion point down. In either > case, I need to anticipate that this might not be the first / only deletion > and that there will be gaps in the item numbering sequence. So I need to > test for an item''s existence before I pipe the line above (with the > appropriate substitutions for ListlineY) into the page. > > Questions... > > To iterate through the list, will I be using Ruby, Javascript, or what? > > Is there a simple existence test? I''m hoping for something like if > item#{x}.exists? > > Am I on the right track here? > > Thanks in advance for any assistance / advice. Advice on a good book to put > me on track would be especially appreciated! > > Best regards, > > Bill > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Bill Walton
2006-Apr-10 17:04 UTC
[Rails] RJS newbie - need help with iterating / existence test
Hi Ed, Thanks. I''ll give it a shot. Best regards, Bill ----- Original Message ----- From: "Edward Frederick" <epfrederick@gmail.com> To: <rails@lists.rubyonrails.org> Sent: 2006-04-10 11:09 AM Subject: Re: [Rails] RJS newbie - need help with iterating / existence test Hi Bill, Been meaning to write you back on the previous emails, no time yet--soon, hopefully. In short, you can just use Ruby in your RJS template for iteration. Generic Example: @items.each do |item| page << "$(#{item.id}).className = ''#{(some condition on item) ? ''class1'' : ''class2''}'';" end Cheers, Ed -- Ed Frederick - edwardfrederick.com On 4/10/06, Bill Walton <bill.walton@charter.net> wrote:> > Greetings! > > I''m new to Ajax and am trying to get a basic app working as a vehicle for > learning it. The app is an extension of the cookbook tutorial. My intent > is to be able to add a list of ingredients for a recipe. The page to add > ingredients uses Ajax. As ingredients are added to the page they''re > displayed in a "green-bar" format like that used in the Depot tutorial. > > Ingredients are added to the page using form_remote_tag to update a <div> > that contains 3 <span>s: one for the ingredient name, one for quantity,and> one that contains a link to delete the ingredient. The delete link uses > link_to_remote which in turn uses an RJS view to remove the item from the > page. > > The stage I''m at now is that, when an item is removed from the page, it > messes up the green-bar effect. So I need to iterate through the <div>s, > changing their CSS class. > > I''ve got the change to class working (thanks to help from Ed Frederick) ona> single item with: > > page << "$(item3).className = ''Listline1'';" > > (In figuring this out, I''m entering 3 items, deleting the 2nd, thenchanging> the class of item3 which is now 2nd in the list) > > So now I to figure out how to iterate through the remaining items, eitherby> starting at the top or working from the deletion point down. In either > case, I need to anticipate that this might not be the first / onlydeletion> and that there will be gaps in the item numbering sequence. So I need to > test for an item''s existence before I pipe the line above (with the > appropriate substitutions for ListlineY) into the page. > > Questions... > > To iterate through the list, will I be using Ruby, Javascript, or what? > > Is there a simple existence test? I''m hoping for something like if > item#{x}.exists? > > Am I on the right track here? > > Thanks in advance for any assistance / advice. Advice on a good book toput> me on track would be especially appreciated! > > Best regards, > > Bill > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails