Home  >  Article  >  Web Front-end  >  Simple front-end js ajax shopping cart framework (getting started)_javascript skills

Simple front-end js ajax shopping cart framework (getting started)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:00:141294browse

I really have nothing to do at the company today, so I suddenly thought of writing down the front-end framework of the shopping cart in the mall. Of course, I only have the addition, deletion, modification, and query of the shopping cart here. Maybe the writing is not that perfect, but the most important thing is an introduction. I hope JS experts can give me some suggestions so that I can reach a higher level.
HOHO~~~ Let’s start:
Js:

Copy code The code is as follows:

//为了省事,就没写自己的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 = ""+
""+
""+
""+
""+
""+
""+
"";
for(var i =0;i{
html += "
"+
""+
""+
""+
""
""
"";
}
html ="
"+
"书啊"+
"
"+
"书名"+
"
"+
" 单价"+
"
"+
" 数量"+
"
"+
" 操作"+
"
"+
""+
"
"+
data[i].Name+
"
"+
"¥"+data[i].Money+
"
"+
"
"+
""+
" "title=\"数量减一\" class=\"cut\" onclick=\"Jusoc.UI.ShoppingCart.Minus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])\">"+
"
"
"
"
"Remove From Cark"
"
" ;
document.body.innerHTML =html;
}
function AddToPanel(data) {
//This is to add an item to the shopping cart to modify the frontend style
var obj = document .getElementById("sm");
var html = ""
""
" "
""
data.Name
""
""
"¥" data.Money
""
""
"
"
""
" "title="Quantity minus one" class="cut" onclick="Jusoc.UI.ShoppingCart. Minus(1,this.parentNode.childNodes[0].value,this.parentNode.childNodes[0])">"
"
"
""
""
"Remove From Cark< /span>"
"";
var row = obj.insertRow(1);
row.innerHTML = html;
return;
obj.childNodes[0 ].innerHTML = html;
}
function UpdatePanel(obj, count) {
//Here are the modification operations to add or subtract from the shopping cart
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: The display page here is just a demo. If necessary, you can customize it yourself.
 HTML:
Copy code The code is as follows:













购物车
总金额:¥






修改成功!



您的商品总金额为¥@Model.Total.ToString("0.00")



关闭






Add one to Shopping Cart





ashx:这个我就不就木有必要黏贴出来了,根据自己的业务去写额。

总结:OK,搞定!!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn