phonegap+emberjs+python开发手机商城,html5实现本地购物车类~
2014-05-21 09:29
661 查看
目前开发项目需要为商城做一个APP,无奈之前玩过android却不甚精通,最终选择phonegap来实现app。
因为购物车分登陆前和登陆后两种情况处理,登录前必然利用到本地存储,而基于phonegap的开发本地存储是使用Html5的localstorage特性实现的。特分享与此~
因为购物车分登陆前和登陆后两种情况处理,登录前必然利用到本地存储,而基于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; } }
相关文章推荐
- phonegap+emberjs+python手机店发展,html5实现本地车类别~
- iOS开发在js中调用oc可以实现在HTML5中调用手机系统的相册,拍照等。
- JS+HTML5手机开发之滚动和惯性缓动实现方法分析
- 很早写的用HTML5 JQUERYMOBILE PHONEGAP来实现的手机照相
- PhoneGap开发初体验:用HTML5技术开发本地应用
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 基于HTML5 Bootstrap搭建的后台模板,分页,模糊查询已经全部JS实现,无需编码,嵌入数据即可开发,内置8款皮肤,欧美风格,非常好用!
- HTML5商城开发二 通过位移实现拖动效果
- [HTML] 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- Python手机开发调用DLL实现部分ADB功能
- 用phonegap打包html5程序(手机app开发html5 juqery moblie phonegap系列二)
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- HTML5商城开发五 实现返回页面顶部
- Html5系列(十一)store.js - 轻松实现本地存储(LocalStorage)
- PhoneGap开发初体验:用HTML5技术开发本地应用
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- PhoneGap或者Cordova框架下实现Html5中JS调用Android原生代码
- [分享黑科技]纯js突破localstorage存储上线,远程抓取图片,并转码base64保存本地,最终实现整个网站所有静态资源离线到用户手机效果却不依赖浏览器的缓存机制,单页应用最新黑科技