app.js http://code.kik.com/app/2/index.html
(function(App,$){ var appLoad = App.load; var appBack = App.back; function getUrlPath(args, options, callback,url){ var path = null; if(typeof args === "string"){ path = args; }else if(typeof options === "string"){ path = options; }else if(typeof callback === "string"){ path = callback; }else if(typeof url === "string"){ path = url; } return path; } App.load = function(pageName, args, options, callback,url){ var url = getUrlPath(args, options, callback,url); pushState(pageName); // // 是否有同名的页面已经加载 if (App._Pages.has(pageName)) { appLoad(pageName, args, options, callback); return; } // 解析传入的html function parsePage(html){ return html; // TODO we can enhance this } $.get(url,function(res){ var pageHtml = parsePage(res); $("body").append(pageHtml); var page = $(pageHtml).appendTo("body"); App.add(pageName,$("[data-page=‘"+pageName+"‘]")[0]); appLoad(pageName, args, options, callback); }); }; var internalBack = false; App.back = function(pageName, callback){ internalBack = true; appBack(pageName, callback); history.back(); }; function pushState(pageName){ window.history.pushState({name:pageName},"","#/"+pageName); } window.onpopstate = function(event){ var state = event.state; console.log(state); if(!internalBack){ appBack(); } internalBack = false; } })(window.App,$);
原文地址:http://antlove.blog.51cto.com/10057557/1894052