HTML5技术

【移动前端开发实践】从无到有(统计、请求、MVC、模块化)H5开发须知 - 叶小钗(7)

字号+ 作者:H5之家 来源:H5之家 2015-09-28 18:22 我要评论( )

1 define(['AbstractModel', 'AbstractStore', 'cUser'], function (AbstractModel, AbstractStore, cUser) { ERROR_CODE = { 4 'NOT_LOGIN': '00001' 5 }; getUs = function () { 9 var us = 'webapp' ; (_.getUrl

1 define(['AbstractModel', 'AbstractStore', 'cUser'], function (AbstractModel, AbstractStore, cUser) { ERROR_CODE = { 4 'NOT_LOGIN': '00001' 5 }; getUs = function () { 9 var us = 'webapp'; (_.getUrlParam().us) { 14 us = _.getUrlParam().us; 15 } 16 return us; 17 }; BaseModel = _.inherit(AbstractModel, { 20 21 initDomain: function () { 22 var host = window.location.host; .domain = host; (host.indexOf('yexiaochai.baidu.com') != -1) { 28 this.domain = 'xxx'; 29 } (host.indexOf('baidu.com') == -1) { 33 this.domain = 'xxx'; 34 } (host.indexOf('xxx.baidu.com') != -1 || host.indexOf('xxx.baidu.com') != -1) { 38 this.domain = 'api.xxx.baidu.com'; 39 } 40 41 }, 42 43 propertys: function ($super) { 44 $super(); .initDomain(); .path = ''; .cacheData = null; 51 this.param = { 52 head: { 53 us: getUs(), 54 version: '1.0.0' 55 } 56 }; 57 this.dataType = 'jsonp'; .errorCallback = function () { }; .pushValidates(function (data) { .baseDataValidate(data); 64 }); 65 66 }, baseDataValidate: function (data) { 70 if (!data) { 71 window.APP.showToast('服务器出错,请稍候再试', function () { 72 window.location.href = 'xxx'; 73 }); 74 return; 75 } (_.isString(data)) data = JSON.parse(data); ; (data.errno == ERROR_CODE['NOT_LOGIN']) { 82 cUser.login(); 83 } (data.errno == xxxx) { 87 this.errorCallback(); ; 89 } (window.APP && data && data.msg) window.APP.showToast(data.msg, this.errorCallback); ; 95 }, 96 97 dataformat: function (data) { 98 if (_.isString(data)) data = JSON.parse(data); 99 if (data.data) return data.data; 100 return data; 101 }, 102 103 buildurl: function () { .protocol + '://' + this.domain + this.path + (typeof this.url === 'function' ? this.url() : this.url); 105 }, 106 107 getSign: function () { 108 var param = this.getParam() || {}; 109 return JSON.stringify(param); 110 }, 111 112 onDataSuccess: function (fdata, data) { 113 if (this.cacheData && this.cacheData.set) 114 this.cacheData.set(fdata, this.getSign()); 115 }, getParam: function () { 119 var param = _.clone(this.param || {}); .param; 125 }, 126 127 execute: function ($super, onComplete, onError, ajaxOnly, scope) { 128 var data = null; 129 if (!ajaxOnly && !this.ajaxOnly && this.cacheData && this.cacheData.get) { 130 data = this.cacheData.get(this.getSign()); 131 if (data) { 132 onComplete(data); 133 return; 134 } 135 } $super(onComplete, onError, ajaxOnly, scope); 139 } 140 141 }); Store = { 145 RequestStore: _.inherit(AbstractStore, { propertys: function ($super) { 148 $super(); 149 this.key = 'BUS_RequestStore'; } 152 }) 153 }; { requestModel: _.inherit(BaseModel, { propertys: function ($super) { 161 $super(); 162 this.url = '/url'; 163 this.ajaxOnly = false; 164 this.cacheData = Store.RequestStore.getInstance(); 165 } 166 }) 167 }; 168 });

业务封装

 

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

相关文章
  • [移动端] IOS下border-image不起作用的解决办法 - 小路_同学

    [移动端] IOS下border-image不起作用的解决办法 - 小路_同学

    2017-05-02 12:04

  • 前端工具的安装 - 韩子卢

    前端工具的安装 - 韩子卢

    2017-05-02 08:00

  • 【Vue 入门】使用 Vue2 开发一个展示项目列表的应用 - zhangjk

    【Vue 入门】使用 Vue2 开发一个展示项目列表的应用 - zhangjk

    2017-04-30 16:00

  • 在Delphi下使用迅雷APlayer组件进行免注册开发 - Delphi力量

    在Delphi下使用迅雷APlayer组件进行免注册开发 - Delphi力量

    2017-04-28 15:00

网友点评
"