前端学习--promise

'use Strict';导入。新建函数,利用if,else分别书写resolve和reject两个方法。采用 return返回函数的方法暴露内容。

//两桶读取数据,转为json数据	
	    	function ReaderModel(){
	    	  var Chapter_id;
	    	  var ChapterTotal;
	    		var init = function(UIcallback){
//	    			getFictionInfo(function(){
//	    				getCurChapterContent(Chapter_id,function(data){
//	    					UIcallback &&UIcallback(data);
//	    				});
//	    			})
                    getFictionInfoPromise().then(function(d){
                     	return getCurChapterContentPromise();
                     }).then(function(data){
                     	UIcallback &&UIcallback(data);
                     });
	    		}
	    		var getFictionInfo = function (callbcak){
	    			/*服务器端接口获得数据*/
	    			$.get('data/chapter.json',function(data){
	    				/*获得章节信息之后的回调*/
	    			Chapter_id= Util.StorageGetter("last_chapter_id");
	    			if(Chapter_id ==null)	{
	    				Chapter_id = data.chapters[1].chapter_id;
	    			}
	    				ChapterTotal=data.chapters.length;
	    				callbcak && callbcak();
	    			},'json');
	    		}
	    		//新建异步处理函数
	    	var getFictionInfoPromise = function(){
	    		return new Promise( function(resolve,reject){
	    		     /*服务器端接口获得数据*/
	    		$.get('data/chapter.json',function(data){
	    				/*获得章节信息之后的回调*/
	    		if(data.result == 0)
	    		{
	    			Chapter_id= Util.StorageGetter("last_chapter_id");
	    			if(Chapter_id ==null)	{
	    				Chapter_id = data.chapters[1].chapter_id;
	    			  }
	    	          ChapterTotal=data.chapters.length;
	    			  resolve();
	    			}else
	    			   {reject();}//失败返回reject,
	    			
	    		 },'json');	
	    		     	
	    		});
	    		}
	    	//采用的Promise的代码优化。开发
	    		 var getCurChapterContentPromise = function(){
	    		 	return new Promise(function(resolve,reject){
	    		 		$.get('data/data'+Chapter_id +'.json',function(data){
	                    if(data.result ==0){//判断服务器的状态
	                    	var url = data.jsonp;//数据地址
	                    	Util.getBSONP(url,function(data){
	                    	
	                    	  
	                    	  resolve(data);
	                    	});
	                    }else{
	                    	reject();
	                    }
	    			    },'json');
	    		 	});
	    		 }
	    		//获得当前详情内容的对象函数。
	    		var getCurChapterContent = function(chapter_id,callback){
	    			/*获得data数据*/
	    			$.get('data/data'+chapter_id +'.json',function(data){
	                    if(data.result ==0){//判断服务器的状态
	                    	var url = data.jsonp;//数据地址
	                    	Util.getBSONP(url,function(data){
	                    	
	                    	  callback && callback(data);
	                    	});
	                    }
	    			},'json');
	    		}
	    		
	    		
	    	    var preChapter = function(UIcallback){
                	Chapter_id = parseInt(Chapter_id,10);
                	if(Chapter_id == 0)
                	{
                		return ;
                	}
                    Chapter_id -=1;
                	getCurChapterContent(Chapter_id,UIcallback);
                	Util.StorageSetter("last_chapter_id",Chapter_id);
                }
                var nextChapter = function(UIcallback){
                	Chapter_id = parseInt(Chapter_id,10);
                	if(Chapter_id == ChapterTotal)
                	{
                		return ;
                	}
                	Chapter_id +=1;
                	getCurChapterContent(Chapter_id,UIcallback);
                	Util.StorageSetter("last_chapter_id",Chapter_id);
                }
	    		return {
	    			init :init,
	    			preChapter:preChapter,
	    			nextChapter:nextChapter
	    		}
	    	}

猜你喜欢

转载自blog.csdn.net/m0_37727198/article/details/81669383