HTML5 移动Web App阅读器-5(页面和服务器通信代码开发)

来源:互联网 发布:一厢情愿的感情知乎 编辑:程序博客网 时间:2024/05/22 15:08

通过接口,请求地址,返回加密的json数据。
防止数据被扒走。
页面展示和上下翻页
在取得上下翻页的数据之前,要先获得章节摘要的信息
获取章节的内容->获得jsonp的地址->然后根据地址拿到bash64的数据。->解码,前端展示
解决chrome跨域请求,开服务器http-server

function main(){        //todo 整个项目的入口函数        var readerModel = ReaderModel();        readerModel.init();        EventHanlder();    }    // 数据层    function ReaderModel(){        //todo 实现和阅读器相关的数据交互方法        var Chapter_id;        var init = function(){            getFictionInfo(function(){                getCurChapterContent(Chapter_id,function(){                });            })        }        // 获得章节的信息        var getFictionInfo = function(callback){            $.get('data/chapter.json',function(data){                // todo 获得章节信息之后的回调                Chapter_id = data.chapters[1].chapter_id;                callback && callback();            },'json');        }        //获得章节的内容        var getCurChapterContent = function(chapter_id,data){            $.get('data/data' + chapter_id + '.json',function(data){                // 确定服务器端ok                if(data.result == 0){                    var url = data.jsonp;                    Util.getBSONP(url,function(data){                        callback && callback(data);                    })                }            },'json')        }        //暴露init方法        return{            init : init        }    }

这里写图片描述
异步:回调函数会使代码的嵌套层次过深
避免使用回调函数:

  1. 消息通知
  2. ES6 Promise,Generator
0 0
原创粉丝点击