English 中文(简体)
购物 Cart Plugin: Implementing Session Management
原标题:Jquery Shopping Cart Plugin: Implementing Session Management

Am using jquery shopping cart http://plugins.jquery.com/project/smartcart2 Can anyone help me in implementing onAdded, onRemoved, onUpdated methods in order to make Ajax call to maintain session on the server side. I make the Ajax request on the server whenever an object is added/removed/updated and get the data back in JSON format. I update the qty like obj.attr("qty", eval(msg)[1]); with the updates from server. However, if i hit refresh or the table data is repopulated the cart items are no longer there. So, the question really is how to populate the data from session so that the product would still remain in the shopping cart upon refresh etc.

$( #SmartCart ).smartCart({ 
onAdded: function(pObj,quantity){ cartAdded(pObj,quantity);}, 
onRemoved: function(pObj){ cartRemoved(pObj);},
onUpdated: function(pObj,quantity){ cartUpdated(pObj,quantity); },
});

function cartAdded(obj,qty){
var product_id = obj.attr("pid");
var quantity = qty; 
// Ajax calls for adding product to cart
function(pObj,quantity){
    cartAdded(pObj,quantity);}
}

function cartRemoved(obj){
var product_id = obj.attr("pid");
// Ajax call for removing product from cart
}

function cartUpdated(obj,qty){
var product_id = obj.attr("pid");
var quantity = qty; 
// Ajax call for updating product on cart
}

function cartAdded(obj,qty){
            var partNum = obj.attr("partNumber");
            var quantity = qty;                
         $.ajax({
        type:  POST ,
        url: "json/sessionManager",
        data : "partNum=" + partNum + "&qty=" + quantity,
        dataType: "text/json",
        success: function(msg){
            obj.attr("qty", msg[1]);
        },
        error: function(httpRequest, textStatus, errorThrown) {
           alert("status=" + textStatus + ",error=" + errorThrown);
        }
    });  
        }

I would highly appreciate recommendations around the same.

最佳回答
  1. Implement your cart on server side.
  2. Keep your cart objects (products) in a collection or something.
  3. 每当Ajax要求到达服务器方时:

    • Get data (product_id,quantity, ...etc) from request.
    • Get operation type (add,remove ..).
    • Check for data validity.
    • Do required server side business (update db, call web-service etc).
    • If everything is fine add/remove specified product to/from your collection.
    • Convert your server side cart to JSON and write to response of Ajax call.
  4. 每当一名JSON的回复到达客户方时:

    • Update your cart with new data.

Ajax部分。 http://api.jquery.com/jQuery.ajax/“rel=“nofollow” 履历

$.ajax({
   type: "POST",
   url: "cart.jsp",
   data: "p_id=SKU001&quantity=4",
   success: function(msg){
     alert( "FOUR SKU001 ADDED TO CART");
   }
 });

Edit: Ooh i see. product_id is undefined means your obj.attr("pid"); is not working.

This plug-in uses hidden HTML inputs for product definitions. (plain stupid if you ask me) This inputs have some pseudo attributes which you can get by obj.attr("pid");. If there is no input in your form or your input does not have that pseudo attributes your code fails.

附录 ATTRIBUTES。

例如:

<div id="SmartCart" class="scMain">
  <input type="hidden" pimage="products/product1.jpg" pprice="2299.99" pdesc="" 
    pcategory="Computers" pname="Apple MacBook Pro MA464LL/A 15.4" pid="100">

  <input type="hidden" pimage="products/product6.jpg" pprice="2699.99" pdesc="" 
    pcategory="Computers" pname="Sony VAIO 11.1&quot; Notebook PC" pid="101">
  <input type="hidden" pimage="products/product3.jpg" pprice="550.00" pdesc="" 
    pcategory="Cameras" pname="Canon Digital Rebel" pid="102">
</div>

作者的文件:

说明:标注bold <>/strong>是描述该产品的pseudo属性。 同产品名称、价格、描述等。

  • pid : Product ID
  • pname : Name of the product
  • pdesc : Description of the product
  • pprice : Price of the product
  • pimage : Product image source
  • pcategory : Category of the product

You can add more product details by adding new attributes to the input element, and so you can show them on product list or cart by editing the template. You can customize the pseudo attribute names, and configure it on the plug-in file on the Attribute Settings section.

Edit2:我认为,在我的建议中,你有困难。

每当一名JSON的回复到达客户方时:

<><>>>> 载有新数据的更新

Hidden inputs below are your data. Open another question on stackoverflow and ask

如何修改(修订、更新和删除)在“ j”中的这种投入

<div id="SmartCart" class="scMain">
  <input type="hidden" pimage="products/product1.jpg" pprice="2299.99" pdesc="" 
    pcategory="Computers" pname="Apple MacBook Pro MA464LL/A 15.4" pid="100">

  <input type="hidden" pimage="products/product6.jpg" pprice="2699.99" pdesc="" 
    pcategory="Computers" pname="Sony VAIO 11.1&quot; Notebook PC" pid="101">
  <input type="hidden" pimage="products/product3.jpg" pprice="550.00" pdesc="" 
    pcategory="Cameras" pname="Canon Digital Rebel" pid="102">
</div>

由于在解决你第一个在同一个职位上询问另一个职位的问题之后,是坏的做法

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签