Hi guys, I am trying to create a route which will handle any route provided. I have done it using wildcard *. Now my problem is while setting template and subscribing. I have some conditions which is going to decide which template to use and what to subscribe.
Here is my code:
waitOn: function(){
var urlParameters = this.params.path.split('/');
var nonMenu = new Array();
/**
* @param {index}
* @param {parent}
* @return {boolean}
* Returns true for url formed by other parameters is true. If not return false
*/
function urlCheck(index, parent){
for(var i = index-1; i>=0; i-- ){
var menu = Menus.find({alias: urlParameters[i]}).fetch()[0];
if(menu._id._str == parent._str){
if(i == 0)
return true;
parent = menu.parent.id;
} else {
return false;
}
}
}
for(var i = urlParameters.length-1; i>=0; i-- ){
var menu = Menus.find({alias: urlParameters[i]}).fetch()[0];
if(menu){
var found = (i != 0)?urlCheck(i, menu.parent.id):true;
break;
} else {
nonMenu.push(urlParameters[i]);
continue;
}
}
if(found && nonMenu.length){
var item = nonMenu[0];
menu.menuItem.type = '';
return checkUrlArraySync('checkUrlArray');
} else {
if(!(found && !nonMenu.length))
menu.menuItem.type = '404';
return directMenu();
}
function checkUrlArraySync(name){
var dep = new Deps.Dependency;
var ready = false;
var handle = {
ready: function() {
dep.depend();
return ready;
}
};
Meteor.call(name, item, function (error, result) {
if (error)
console.log(error);
else {
if(result.length){
if(nonMenu.length>1 && !checkCategoryStructure(result[0].category, nonMenu)){
menu.menuItem.type = '404';
}
menu.menuItem.type = result[0].type;
menu.menuItem.id = result[0]._id;
directMenu();
} else {
console.log('404');
menu.menuItem.type = '404';
directMenu();
}
}
ready = true;
dep.changed();
});
return handle;
}
function checkCategoryStructure(parent, nonMenu){
var index = 1; var ret = true;
for(var i = nonMenu.length-1; i > 0; i--){
Meteor.call('checkParentCategory', parent, function (error, res) {
if(error){
console.error(error);
} else if(res.length && (nonMenu[index] == res[0].alias)){
parent = res[0].category;
} else {
console.log('404 Not Found')
ret = false;
}
if(i == 1){
if(menu.menuItem.id._str != res[0]._id._str){
console.log('404 Not Found - Wrong Parent')
ret = false;
}
}
index++;
});
}
if(index == nonMenu.length){
return ret;
}
}
function directMenu(){
this.root = _root;
switch (menu.menuItem.type){
case 'article':
Router.setTemplateNameConverter(function () { return 'staticPage'; });
Meteor.subscribe('menuArticle', menu.menuItem.id);
break;
case 'category':
Router.setTemplateNameConverter(function () { return 'categoryPage'; });
Meteor.subscribe('menuCategory', menu.menuItem.id);
break;
case '404':
Router.setTemplateNameConverter(function () { return 'notFound'; });
break;
}
console.log('set template: ', menu.menuItem.type);
}
}
Now in when it enters in if condition it is loading… loading… loading… and loading… continuously and printing set template:
in infinite loop in console. It never renders template which is set for it.
Please let me know where have I mistaken or if you think is not good way, please tell me the best way to do it.