Skip to content

Commit

Permalink
fix server render bug
Browse files Browse the repository at this point in the history
  • Loading branch information
brandnewera committed Sep 4, 2013
1 parent 16dd904 commit eea794d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 57 deletions.
27 changes: 15 additions & 12 deletions sumeru/server_client/server_router.js
Expand Up @@ -30,19 +30,23 @@ var runnable =function(fw){
sRouter.__reg('setServerRender', function(runServer) {
_runServerRender = runServer;
});
var routing_map = {}
var routing_map = {};

var check_routeing = function(path){
var check_routeing = function(path){//for server router using.
if (!_runServerRender)
return null;
if (!routing_map[path]){
routing_map[path] = fw.uri.getInstance(path);
}
if ( routing_map[path].controller == null){//上传文件流程
return null;
}else{
return routing_map[path];
}

}
return routing_map[path].controller;
}
};
var __routeing = function(path,dom,callback){
var uriParts = routing_map[path];
var uriParts = fw.uri.getInstance(path);//routing_map[path];

//处理session change
isSessionChange = true;//lastSession != uriParts.session;
Expand All @@ -63,8 +67,7 @@ var runnable =function(fw){

return sDispatch(uriParts.controller, uriParts,path,dom,callback);

}

};
var findController = function(path){
var pattern , find;
var routeMap =fw.router.getAll();
Expand Down Expand Up @@ -111,14 +114,14 @@ var runnable =function(fw){
readyhtml = dom.replace("</body>",readydom+"</body>");
}
callback(readyhtml);
}
};
fw.controller.getServerInstance(identifier, uriParts,path,constructor,__onFinish);

return true;
}
};
var finishServerRender = function(path,dom,callback){
__routeing(path,dom,callback);
}
};

sRouter.__reg('finishServerRender', finishServerRender, 'private');
sRouter.__reg('check_routeing', check_routeing, 'private');
Expand All @@ -130,7 +133,7 @@ var runnable =function(fw){



}
};

if(typeof module !='undefined' && module.exports){
module.exports = runnable;
Expand Down
113 changes: 68 additions & 45 deletions sumeru/src/uri.js
Expand Up @@ -18,43 +18,52 @@ var runnable = function(fw,runfromServer){
this.controller = null;
this.original = null;
this.session="";
this.type;
this.contr_argu = [];
}
};
var routerObj = null;

var parseFileFromUrl = function(filePath){// all files
//1. ?后的一切都不是file的name
if ( filePath.indexOf('?') != -1 ) {
filePath = filePath.substring(0,filePath.indexOf("?"));
}
//文件直接读取,使用baseurl,已经彻底解决此问题
if (filePath.match(/\.\w+$/) ) {
if (filePath.indexOf('.html/') != -1){
filePath = filePath.substring(filePath.indexOf(".html/")+5);
}
//检测controller的去减
// if (!routerObj){
// routerObj = fw.router.getAll();
// }

}
//2. /的前面如果有.html也舍弃后面的
if ( filePath.indexOf('.html/') != -1) {
filePath = filePath.substring(0,filePath.indexOf('.html/')+5);
}
//3. /前面没有.html,且匹配了定义的router,则返回index.html
if ( !filePath.match(/\.\w+$/) ) {//文件直接读取
filePath = "/index.html";
}

return filePath;
}
// var parseFileFromUrl = function(filePath){// all files
// //1. ?后的一切都不是file的name
// if ( filePath.indexOf('?') != -1 ) {
// filePath = filePath.substring(0,filePath.indexOf("?"));
// }
// //文件直接读取,使用baseurl,已经彻底解决此问题
// if (filePath.match(/\.\w+$/) ) {
// if (filePath.indexOf('.html/') != -1){
// filePath = filePath.substring(filePath.indexOf(".html/")+5);
// }
// //检测controller的去减
// // if (!routerObj){
// // routerObj = fw.router.getAll();
// // }
//
// }
// //2. /的前面如果有.html也舍弃后面的
// if ( filePath.indexOf('.html/') != -1) {
// filePath = filePath.substring(0,filePath.indexOf('.html/')+5);
// }
// //3. /前面没有.html,且匹配了定义的router,则返回index.html
// if ( !filePath.match(/\.\w+$/) ) {//文件直接读取
// filePath = "/index.html";
// }
//
// return filePath;
// };
var parseFromUrl = function(filePath){
//0. 去掉#号
if (filePath.indexOf("#")!= -1) {
filePath = filePath.replace("#","");
}
};
filePath = filePath.replace(/\/+/g,"/");

// hack to support no base_url router eg. debug.html/js/src/sumeru.js could be js/src/sumeru.js
if (filePath.indexOf('.html/') != -1){
if (filePath.match(/\.\w+$/) || filePath.match(/\.\w+\?/) ) {//static file
filePath = filePath.substring(filePath.indexOf(".html/")+5);
}
}

//1. ?后的一切都不是file的name
var _filePath = filePath;
var params="",controller="";
Expand All @@ -72,7 +81,12 @@ var runnable = function(fw,runfromServer){
controller = _filePath;
_filePath = "/";
}

//判断静态文件
if (filePath.match(/\.\w+/) && !filePath.match(/\.html/)) {//static file
this.controller = null;
this.path = _filePath;
return this;
}
if (!routerObj){
routerObj = fw.router.getAll();
}
Expand All @@ -89,7 +103,11 @@ var runnable = function(fw,runfromServer){
}
if (q>-1){// HAS MATCH ROUTER
if (runfromServer && !routerObj[q].server_render) {
fw.log(routerObj[q],"no server render...");
if (routerObj[q].type === 'file'){
fw.log("router:fileUpload matche.",routerObj[q].path);
}else{
fw.log("router:server_render is false.",routerObj[q].path);
}
controller = null;
}else{
if (longest == 0){//最长的就是空白
Expand All @@ -107,6 +125,11 @@ var runnable = function(fw,runfromServer){
contr_argu[0] = controller;
}
}

if (routerObj[q].type==='file'){
this.type = routerObj[q].type;
controller = null;//上传文件不需要server渲染
}
}else{// NO MATCH ROUTER
controller = null;
contr_argu.push("");
Expand All @@ -121,34 +144,34 @@ var runnable = function(fw,runfromServer){
var session = (sessionObj.substring(1,sessionObj.length - 1));

//TODO REMOVE paramstring LATER
return {path:_filePath,params:paramsObj,controller:controller,session:session,contr_argu:contr_argu};

this.path = _filePath;
this.params = paramsObj;
this.controller = controller;
this.session = session;
this.contr_argu = contr_argu;
return this;//{path:_filePath,params:paramsObj,controller:controller,session:session,contr_argu:contr_argu};


}
// var parseSessionFromUrl = function(){
//
// }
};

_uri.prototype = {
parseFromUrl :parseFromUrl,
init : function(filePath){
this.original = filePath;
var obj = parseFromUrl(filePath);
this.path = obj.path;
this.params = obj.params;
this.controller = obj.controller
this.session = obj.session;
this.contr_argu = obj.contr_argu;
this.parseFromUrl(filePath);

},

}
};
fwuri.__reg("getInstance",function(url){
var uri = new _uri();
uri.init(url);
return uri;
});
fwuri.__reg("parseFileFromUrl",parseFileFromUrl);
// fwuri.__reg("parseFileFromUrl",parseFileFromUrl);

}
};

if(typeof module !='undefined' && module.exports){
module.exports = function(fw){
Expand Down

0 comments on commit eea794d

Please sign in to comment.