Skip to content

Instantly share code, notes, and snippets.

@mushfiq
Created September 25, 2012 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mushfiq/3780176 to your computer and use it in GitHub Desktop.
Save mushfiq/3780176 to your computer and use it in GitHub Desktop.
Javascript url based pattern
var app = this.app || {};
// Application Settings
app.settings = {
'ajax' : {
'timeout' : 50000,
'delay' : 0
}
}
// URLs
app.urls = {
}
// Global Variables
app.globals = {
'global_varibale1' : '',
'global_varibale2' : ''
}
app.init = function () {
// Define console.log if it is not supported
try {
console.log('Running app...');
} catch (err) {
console = {};
console.log = function(text) {
console.log = function(text) {
// leave it
}
}
}
};
// Global objects
//------------------------
var date = new Date();
var currentYear = date.getYear() + 1900;
var currentMonth = date.getMonth() + 1;
var currentDate = date.getDate();
//utility method
var utility_mthod_1 = function(text) {
return text;
}
app.Global = {
}
// Example APP 21(based on path)
app.ExamplePage1 = {
init:function () {
//write jjs events here!
}
}
// Example APP 2(based on path)
app.ExamplePage2 = {
init:function () {
app.ExamplePage2.method_1("one","two");
},
method_1:function (i, j) {
if (confirm('Are you sure you want to delete this CMS?')) {
rowElement.slideUp('slow');
$.ajax({
//handle ajax call here!
});
}
}
}
app.routes = [
{
url:new RegExp("/page/id"),
callback:app.ExamplePage1Page.init
},
{
url:new RegExp("/node/id"),
callback:app.ExamplePage2.init
}
];
app.urlResolver = function (url) {
var count = app.routes.length;
for (var index = 0; index < count; index++) {
var urlpattern = app.routes[index];
if (url.search(urlpattern.url) > -1) {
urlpattern.callback();
break;
}
}
}
$(document).ready(function () {
app.init();
app.urlResolver(location.pathname);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment