AJax技术

简单的前端js+ajax 购物车框架(入门篇)

字号+ 作者:H5之家 来源:H5之家 2017-11-10 14:01 我要评论( )

今天在公司实在没有事做,突然就想到写下商城的购物车的前端框架,当然我这里只有购物车的增删改查,也许写的并不是那么完善,但最重要的是一个入门,也希望js达

今天在公司实在没有事做,突然就想到写下商城的购物车的前端框架,当然我这里只有购物车的增删改查,也许写的并不是那么完善,但最重要的是一个入门,也希望js达人给些建议,好让我更上一个台阶。
HOHO~~~开始咯:
Js:

  复制代码 代码如下:

  //为了省事,就没写自己的js ajax了 用了jquery的,当然你也可以添加到jquery的扩展方法内,哈哈,我太懒了,所以就写这里了。
var _$ = { AJAX: function (urlparm, d, beforecall, successcall) {
$.ajax({
url: "ashx/ajax_shoppingCart.ashx?" + urlparm,
data:d,
dataType:"Json",
type: "POST",
before: beforecall,
success:successcall
});
}
};
(function () {
var Jusoc = {};
Jusoc = {
_inital: function () { window.Jusoc = Jusoc; },
Base: {},
DAO: {},
BLL: {},
UI: {}
}
Jusoc.Base = {
Validate: {
}
}
//AJAX()
Jusoc.DAO = {
Shopping: {
Get: function (beforecall, successcall) {
_$.AJAX("action=get", null, beforecall, successcall);
},
Remove: function (pid, beforecall, successcall) {
_$.AJAX("action=remove", { "pid": pid }, beforecall, successcall);
},
Add: function (pid, pcount, beforecall, successcall) {
_$.AJAX("action=add", { "pid": pid, "pcount": pcount }, beforecall, successcall);
},
Set: function (pid, pcount, beforecall, successcall) {
_$.AJAX("action=set", { "pid": pid, "pcount": pcount }, beforecall, successcall);
}
}
}
Jusoc.BLL = {
Shopping: (function () {
var Data = null;
var isLock = false;
var _successcall = null;
var _beforecall = null;
function Unlock() {
isLock = false;
}
function Lock() {
isLock = true;
if(Data&&Data !=null)
{
Data = null;
}
}
function CallBackFunction(xhr) {
Unlock();
// if (xhr[0] == "ERROR") {
// alert(xhr[1]);
// return;
// }
// else if (xhr[0] == "SUCCESS") {
// Jusoc.BLL.Shopping.SetData(xhr[1]);
// }
Jusoc.BLL.Shopping.SetData(xhr);
if (_successcall != null && _successcall) {
_successcall.call(window, xhr);
}
_successcall = null;
}
function PrepareRequst(beforecall, successcall) {
if (isLock) {
return false;
}
Lock();
if (beforecall != null && beforecall) {
_beforecall = beforecall;
}
if (successcall != null && successcall) {
_successcall = successcall;
}
}
return {
Get: function (beforecall, successcall) {
if(PrepareRequst(beforecall, successcall)==false)return false;
Jusoc.DAO.Shopping.Get(_beforecall, CallBackFunction);
},
Remove: function (pid, beforecall, successcall) {
if(PrepareRequst(beforecall, successcall)==false)return false;
Jusoc.DAO.Shopping.Remove(pid, _beforecall, CallBackFunction);
},
Set: function (pid, pcount, beforecall, successcall) {
if(PrepareRequst(beforecall, successcall)==false)return false;
Jusoc.DAO.Shopping.Set(pid, pcount, beforecall, CallBackFunction);
},
Add: function (pid, pcount, beforecall, successcall) {
if(PrepareRequst(beforecall, successcall)==false)return false;
Jusoc.DAO.Shopping.Add(pid, pcount, _beforecall, CallBackFunction);
},
GetData: function () {
//alert(Data);
return Data;
},
SetData: function (data) { Data = data; },
RemoveData: function () {
if (Data != null && Data)
Data= null;
}
}
})(),
XHR: {
}
}
Jusoc.UI = {
ShoppingCart: (function () {
function Constract() {
Jusoc.BLL.Shopping.Get(null,SetShoppingCart);
}
function SetShoppingCart(data) {
//这里来填充购物车中的数据
var data = Jusoc.BLL.Shopping.GetData();
//这里 先构建 整个的购物车
var html = "<table class=\"shoppingcart-list\" id=\"sm\">"+
"<tr>"+
"<th>"+
"书啊"+
"</th>"+
"<th>"+
"书名"+
"</th>"+
"<th>"+
" 单价"+
"</th>"+
"<th>"+
" 数量"+
"</th>"+
"<th>"+
" 操作"+
"</th>"+
"</tr>";
for(var i =0;i<data.length;i++)
{
html += "<tr>"+
"<td>"+
"<img src=\"ss\" height=\"36px\" width=\"28px\"/>"+
"</td>"+
"<td>"+
data[i].Name+
"</td>"+
"<td>"+
"¥"+data[i].Money+
"</td>"+
"<td>"+
"<div class=\"item-change\">"+
"<input type=\"text\" value='"+data[i].Count+"' />"+
"<span title=\"数量加一\" class=\"add\" onclick=\"Jusoc.UI.ShoppingCart.Plus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span> <span "+
"title=\"数量减一\" class=\"cut\" onclick=\"Jusoc.UI.ShoppingCart.Minus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span>"+
"</div>"+
"</td>"+
"<td>"+
"<span class=\"RemoveLink\" onclick=\"Jusoc.UI.ShoppingCart.Remove(1,this.parentNode.parentNode)\">Remove From Cark</span>"+
"</td>"+
"</tr>";
}
html+="</table>";
document.body.innerHTML+=html;
}
function AddToPanel(data) {
//这里是对 添加一个商品到购物车 来修改前台样式
var obj = document.getElementById("sm");
var html = "<td>"+
"<img src=\"soo\" height=\"36px\" width=\"28px\"/>"+
"</td>"+
"<td>"+
data.Name+
"</td>"+
"<td>"+
"¥"+data.Money+
"</td>"+
"<td >"+
"<div class=\"item-change\">"+
"<input type=\"text\" value='"+data.Count+"' />"+
"<span title=\"数量加一\" class=\"add\" onclick=\"Jusoc.UI.ShoppingCart.Plus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span> <span "+
"title=\"数量减一\" class=\"cut\" onclick=\"Jusoc.UI.ShoppingCart.Minus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\"></span>"+
"</div>"+
"</td>"+
"<td>"+
"<span class=\"RemoveLink\" onclick=\"Jusoc.UI.ShoppingCart.Remove(1,this.parentNode.parentNode)\">Remove From Cark</span>"+
"</td>";
var row = obj.insertRow(1);
row.innerHTML = html;
return;
obj.childNodes[0].innerHTML += html;
}
function UpdatePanel(obj, count) {
//这里是从购物车中 增加 或者 减少 修改操作
obj.value = count;
}
function RemoveFromPanel(child)
{
var obj = document.getElementById("sm");
obj.childNodes[0].removeChild(child);
}
return {
PageLoad: function () {
Constract();
},
Add: function (pid, pcount) {
Jusoc.BLL.Shopping.Add(pid,pcount, null, AddToPanel);
},
Plus: function (pid, pcount, obj) {
pcount = parseInt(pcount) + 1;
Jusoc.BLL.Shopping.Set(pid, pcount, function () { alert("before") }, function (data) { UpdatePanel(obj, pcount) });
},
Minus:function(pid,pcount,obj){
pcount = parseInt(pcount) - 1;
Jusoc.BLL.Shopping.Set(pid,pcount,null,function(data){ UpdatePanel(obj,pcount)});
},
Remove:function(pid,obj){
Jusoc.BLL.Shopping.Remove(pid,null,function(data){ RemoveFromPanel(obj);});
}
}
})()
}
Jusoc._inital();
})()


  Tips:这里的显示页面仅仅是demo,如需要,可以自己定制。
  HTML:

  复制代码 代码如下:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 基于jquery的$.ajax async使用

    基于jquery的$.ajax async使用

    2017-11-11 09:01

  • ajax请求如何携带cookie

    ajax请求如何携带cookie

    2017-11-10 12:03

  • 自己写的Asp.netAjax,Jquery,ExtJs三种Ajax技术框架比较,希望大家提提意见,谢谢。-.NET技术

    自己写的Asp.netAjax,Jquery,ExtJs三种Ajax技术框架比较,希望大家提

    2017-11-10 09:03

  • 基于Ajax技术的掼蛋比赛自动裁判系统设计与实现

    基于Ajax技术的掼蛋比赛自动裁判系统设计与实现

    2017-11-09 17:02

网友点评