Add config.json to make it even easier to configure things

This commit is contained in:
Lim Chee Aun 2013-11-27 23:13:33 +08:00
parent f86f965e98
commit 1ce9f5bbe4
2 changed files with 26 additions and 4 deletions

4
config.json Normal file
View File

@ -0,0 +1,4 @@
{
"customStylesheetURL": null,
"yearLength": 120
}

View File

@ -99,11 +99,29 @@ h1{
$el: document.getElementById('life'), $el: document.getElementById('life'),
yearLength: 120, // 120px per year yearLength: 120, // 120px per year
start: function(){ start: function(){
life.loadConfig(function(config){
if (config.yearLength) life.yearLength = config.yearLength;
if (config.customStylesheetURL) life.injectStylesheet(config.customStylesheetURL);
life.fetch(function(response){ life.fetch(function(response){
var data = life.parse(response); var data = life.parse(response);
var title = life.parseTitle(response); var title = life.parseTitle(response);
life.render(title, data); life.render(title, data);
}); });
});
},
loadConfig: function(fn){
var xhr = new XMLHttpRequest();
xhr.open('GET', 'config.json', true);
xhr.onload = function(){
if (xhr.status == 200) fn(JSON.parse(xhr.responseText));
};
xhr.send();
},
injectStylesheet: function(url){
var link = document.createElement('link');
link.rel = 'stylesheet';
document.body.appendChild(link);
}, },
fetch: function(fn){ fetch: function(fn){
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();