Skip to content

Instantly share code, notes, and snippets.

@sshirokov
Created July 6, 2009 03:58
Show Gist options
  • Save sshirokov/141256 to your computer and use it in GitHub Desktop.
Save sshirokov/141256 to your computer and use it in GitHub Desktop.
The early stages for a framework to render a site using JS-defined fragments
//Example fragments, extra magic withheld
window.fragments = {
name: {
url: context.project_property_url.to_project_url().to_property_url('name'),
location: '.project_title'
},
background: {
_interval: 60000,
intervals: {
url: context.interval_url.to_project_url(),
location: 'div#intervals'
},
totals: {
url: context.project_property_url.to_project_url().to_property_url('duration_string'),
location: '.duration_string'
}
}
};
//From: http://stackoverflow.com/questions/18912/how-to-find-keys-of-a-hash
Object.prototype.keys = function ()
{
var keys = [];
for(i in this) if (this.hasOwnProperty(i))
{
keys.push(i);
}
return keys;
}
//Helpers
function get_url_into_css(url, selector, error) {
if(error == undefined) { error = "Error loading data."; }
$.ajax({
url: url,
type: 'GET',
data: {},
success: function(data, textStatus) {
debug.log("Loaded", data.length, "bytes of data");
var r = $(selector).html(data);
debug.log("Replaced:", r);
},
error: function(xhr, textStatus, errorThrown) {
debug.log("Error [", textStatus, "].");
alert(error);
}
});
}
//Rendering engine
render = {
_is_leaf: function(data, parent) {
var allStrings = true;
$.each(data.keys(), function() {
if(!(typeof(data[this]) == "string")) allStrings = false;
});
return allStrings;
},
_has_meta: function(object, parent) {
return $.grep(object.keys(), function(e) { return e[0] == '_'; }).length > 0;
},
_get_meta_options: function(object) {
var options = {};
$.each($.map(object.keys(),
function(e) {
if(e[0] == '_') return e.substring(1);
else return null;
}),
function() {
options[this] = object["_" + this];
});
return options;
},
_loop_interval: function(render, func, interval) {
setTimeout(function() { render._loop_interval(render, func, interval); }, interval);
func.apply(render, []);
},
act_on_meta: function(object, parent) {
debug.log("Acting on meta options for:", object);
var options = this._get_meta_options(object);
var render = this;
if(options.interval != undefined) {
debug.log("Setting timer for", object, "rendering at", options.interval);
setTimeout(function() { render._loop_interval(render,
function() { this.all(object, parent); },
options.interval); },
options.interval);
}
debug.log("Node options:", options);
},
render_data: function(data, parent) {
debug.log("Rendering:", data, "as leaf data.");
get_url_into_css(data['url'], data['location']);
},
render_object: function(object, parent) {
debug.log("Rendering:", object, "as object.");
this.all(object, parent);
},
render_dispatch: function(object, parent) {
if(typeof(object) == "function") object.apply(parent, []); //Trust in the function
else if(this._is_leaf(object, parent)) this.render_data(object, parent);
else this.render_object(object, parent);
},
all: function(fragments, parent) {
for(frag in fragments) {
if(frag[0] != '_') this.render_dispatch(fragments[frag], fragments);
}
},
act: function(fragments, parent) {
debug.log("Acting on", fragments);
for(frag in fragments) {
if(this._has_meta(fragments[frag])) this.act_on_meta(fragments[frag], parent);
else if(typeof(fragments[frag]) == 'object') this.act(fragments[frag], fragments);
}
},
update: function(path) {
var object = this.fragments;
$.each(path.split('.'),
function() { object = object[this]; });
debug.log("Will update:", object);
this.all(object);
},
main: function(fragments) {
debug.log("Rendering engine loading..");
this.fragments = fragments;
this.all(fragments);
this.act(fragments);
debug.log("Rendering engine loaded...");
}
};
//Rendering engine start
$(document).ready(function() {
render.main.apply(render, []);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment