phonegap+emberjs+python手机店发展,html5实现本地车类别~

商城开发项目,现在需要做出APP,无奈出场前android但不是很精通。最后选择phonegap实现app。

由于之前办理购物车分为登陆和登陆后两种情况,登录前必须充分利用本地存储。而基于phonegap本地存储的发展是使用Html5的localstorage功能实现。

特分享与此~

//内置数组对象成员方法补充,移除元素
Array.prototype.remove=function(dx)
{
    if(isNaN(dx)||dx>this.length){
        return false;
    }
    for(var i=0,n=0;i<this.length;i++)
    {
        if(this[i]!=this[dx])
        {
            this[n++]=this[i]
        }
    }
    this.length-=1
}
            
            
//购物车类定义
cart = function(){
    this.lstore = window.localStorage;
    this.init();
};
cart.prototype={
    init:function(){
        var cart_goods_material=this.lstore.getItem('cart');
        try{
            this.cart_goods=JSON.parse(cart_goods_material);
			if(this.cart_goods==null)
				this.cart_goods=[];
        }
        catch(e){
            this.cart_goods=[];
        }
    },
    getall:function(){                   //获得购物车全部商品列表
        return this.cart_goods;
    },
    insert:function(goods){              //插入商品
	
        var index=this.finds(goods.goods_id);
        if(index>-1){
            this.cart_goods[index].goods_number+=goods.goods_number;
        }else{
            this.cart_goods.push(goods);
                            
        }
        var cart_goods_cooked = JSON.stringify(this.cart_goods);
        this.lstore.setItem('cart',cart_goods_cooked);
        return true;
    },
    update:function(goods_id,data){
        var index=this.finds(goods_id);
        if(index>-1){
            for(var i in data){
                this.cart_goods[index][i]=data[i];
            }   
        }
        var cart_goods_cooked = JSON.stringify(this.cart_goods);
        this.lstore.setItem('cart',cart_goods_cooked);
        return true;
		
    },
    addnum:function(goods_id,num){      //更新购物车商品数量
        var index=this.finds(goods_id);
        var num=this.cart_goods[index]['goods_number']+num;
        this.update(goods_id,{
            'goods_number':num
        });          
    },
    del:function(goods_id){           
       var index=this.finds(goods_id);
        this.cart_goods.remove(index);
	var cart_goods_cooked = JSON.stringify(this.cart_goods);
        this.lstore.setItem('cart',cart_goods_cooked);
        return true;
    },
    finds:function(goods_id){
        var index=-1;
        for(var  i in this.cart_goods ){
            var g=this.cart_goods[i];
            if(g.goods_id==goods_id){
                index = i;
                break;
            }
        }
        return index;
    },
    clear:function(){
        this.lstore.setItem('cart','');
	this.cart_goods=[];
    },
    amount:function(){
        var amount=0;
        for(var  i in this.cart_goods ){
            amount += this.cart_goods[i]['goods_number']*this.cart_goods[i]['goods_price'];
        }
        return amount;
    }
}


版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值