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@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to