zoukankan      html  css  js  c++  java
  • 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;
        }
    }


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

  • 相关阅读:
    RabbitMQ从入门到精通(一)
    MQ的架构作用
    Docker可视化管理工具
    Linux修改war包中文件
    Redis--各个数据类型最大存储量
    linux中直接修改jar包内配置文件
    脚本发布程序
    maven 安装到私服
    HTML基础 text-indent 把文字移出浏览器,隐藏起来
    HTML基础 td valign 设置文本靠上 居中 靠下
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4640767.html
Copyright © 2011-2022 走看看