Mike and I demo an Ajaxified version of the Depot application in the Rails Studio. Part of the demo is a cart in the sidebar which updates when you add a product to it. As well as updating the div, it also highlights the most recently added link item. Right now, we do it with: <script language="JavaScript"> function flash_cart(request) { var id = request.getResponseHeader(''X-Item-Id''); new Effect.Highlight(''cart_item_'' + id, { startcolor: "#66bb66", endcolor: "#114411" }); } </script> <%= link_to_remote ''Add to Cart'', :url => {:action => ''add_to_cart'', :id => product}, :update => ''cart'', :complete => ''flash_cart(request)'' %> The controlled sends down item_id as a header when the object gets added: def add_to_cart product = Product.find(params[:id]) item = @cart.add_product(product) if request.xhr? headers[''X-Item-Id''] = item.object_id render(:action => "display_cart", :layout => false) else redirect_to(:action => ''index'') end end I''d really like to use visual_effect() instead of having my own flash_cart method, but I can''t think of a way of giving it the element ID dynamically. Am I missing something? Cheers Dave
Can you not add the code in your display_cart view? Just put something like <%= visual_effect :highlight, ''cart_item_''+@item.object_id %> in the display_cart template (maybe add some logic to only perform the effect if it''s a xhr request) and since rendering of that view is essentially going to happen when the request is complete it should accomplish the same thing no? Just my quick thoughts. Maybe I''m missing something. :) On 1/10/06, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote:> Mike and I demo an Ajaxified version of the Depot application in the > Rails Studio. Part of the demo is a cart in the sidebar which updates > when you add a product to it. As well as updating the div, it also > highlights the most recently added link item. Right now, we do it with: > > <script language="JavaScript"> > function flash_cart(request) { > var id = request.getResponseHeader(''X-Item-Id''); > new Effect.Highlight(''cart_item_'' + id, > { startcolor: "#66bb66", > endcolor: "#114411" }); > } > </script> > > <%= link_to_remote ''Add to Cart'', > :url => {:action => ''add_to_cart'', :id => product}, > :update => ''cart'', > :complete => ''flash_cart(request)'' > %> > > The controlled sends down item_id as a header when the object gets > added: > > def add_to_cart > product = Product.find(params[:id]) > item = @cart.add_product(product) > if request.xhr? > headers[''X-Item-Id''] = item.object_id > render(:action => "display_cart", :layout => false) > else > redirect_to(:action => ''index'') > end > end > > > I''d really like to use visual_effect() instead of having my own > flash_cart method, but I can''t think of a way of giving it the > element ID dynamically. Am I missing something? > > > Cheers > > > Dave > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
On Jan 10, 2006, at 10:43 AM, Andrew Kaspick wrote:> Can you not add the code in your display_cart view? > > Just put something like <%= visual_effect :highlight, > ''cart_item_''+@item.object_id %> in the display_cart template (maybe > add some logic to only perform the effect if it''s a xhr request) and > since rendering of that view is essentially going to happen when the > request is complete it should accomplish the same thing no? >You''d _think_ that''d work... but I don''t seem to be able to. The generated HTML looks OK, but the effect never triggers.
Can you paste some of the generated html? On 1/10/06, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote:> > On Jan 10, 2006, at 10:43 AM, Andrew Kaspick wrote: > > > Can you not add the code in your display_cart view? > > > > Just put something like <%= visual_effect :highlight, > > ''cart_item_''+@item.object_id %> in the display_cart template (maybe > > add some logic to only perform the effect if it''s a xhr request) and > > since rendering of that view is essentially going to happen when the > > request is complete it should accomplish the same thing no? > > > > You''d _think_ that''d work... but I don''t seem to be able to. The > generated HTML looks OK, but the effect never triggers. > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
On Tuesday 10 January 2006 09:27, Dave Thomas wrote:> On Jan 10, 2006, at 10:43 AM, Andrew Kaspick wrote: > > Can you not add the code in your display_cart view? > > > > Just put something like <%= visual_effect :highlight, > > ''cart_item_''+@item.object_id %> in the display_cart template (maybe > > add some logic to only perform the effect if it''s a xhr request) and > > since rendering of that view is essentially going to happen when the > > request is complete it should accomplish the same thing no? > > You''d _think_ that''d work... but I don''t seem to be able to. The > generated HTML looks OK, but the effect never triggers.don''t you need to tell prototype to evaluate scripts on the way back? I''m not sure how exactly this is done in the rails helper functions, but in prototype it''s something like this: var myAjax = new Ajax.Updater(''id'', ''url'', {method: ''get'', evalScripts: true}); -Jeremy -- Jeremy Kitchen ++ kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org In the beginning was The Word and The Word was Content-type: text/plain -- The Word of Bob. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
evalScripts is set to true by default using the rails helpers On 1/10/06, Jeremy Kitchen <kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org> wrote:> On Tuesday 10 January 2006 09:27, Dave Thomas wrote: > > On Jan 10, 2006, at 10:43 AM, Andrew Kaspick wrote: > > > Can you not add the code in your display_cart view? > > > > > > Just put something like <%= visual_effect :highlight, > > > ''cart_item_''+@item.object_id %> in the display_cart template (maybe > > > add some logic to only perform the effect if it''s a xhr request) and > > > since rendering of that view is essentially going to happen when the > > > request is complete it should accomplish the same thing no? > > > > You''d _think_ that''d work... but I don''t seem to be able to. The > > generated HTML looks OK, but the effect never triggers. > > don''t you need to tell prototype to evaluate scripts on the way back? > > I''m not sure how exactly this is done in the rails helper functions, but in > prototype it''s something like this: > > var myAjax = new Ajax.Updater(''id'', ''url'', {method: ''get'', evalScripts: > true}); > > -Jeremy > > -- > Jeremy Kitchen ++ kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org > > In the beginning was The Word and The Word was Content-type: text/plain > -- The Word of Bob. > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > >