Created
December 13, 2012 17:44
-
-
Save worldsayshi/4278206 to your computer and use it in GitHub Desktop.
Source to Dom mapping: Combinator utilities for creating a callback that inserts elements into the DOM tree once a long running data retrieval operation is done.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> | |
<HTML> | |
<HEAD> | |
<TITLE> | |
Datasource to DOM mapping | |
</TITLE> | |
</HEAD> | |
<BODY> | |
<b>A list of stuff:</b> | |
<ul id="dest"></ul> | |
</BODY> | |
</HTML> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The actual mapping object | |
function Mapping(dest, elementMapping, srcpath) { | |
return function (src) { | |
for(var i=0; i<srcpath.length;i++){ | |
src=src[srcpath[i]]; | |
} | |
//dest.clear(); | |
for(var i=0; i<src.length;i++){ | |
var elem = elementMapping(src[i]); | |
dest.appendChild(elem); | |
} | |
} | |
} | |
// A dummy data request | |
function getDataMaybeAsynchronous (callback) { | |
var data = {}; | |
var list = []; | |
data.list = list; | |
for (var i=0;i<20;i++){ | |
list.push({name:i,content:Math.random()*100}); | |
} | |
callback(data); | |
} | |
// Example use: | |
var destinationList = document.getElementById("dest"); | |
var createListElem = function(cont){ | |
var elem = document.createElement('li'); | |
var text = document.createTextNode(cont.content); | |
elem.appendChild(text); | |
return elem; | |
}; | |
var mapping = Mapping (destinationList, | |
createListElem, | |
["list"]); | |
getDataMaybeAsynchronous(mapping); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Source to Dom mapping | |
description: Combinator utilities for creating a callback that inserts elements into the DOM tree once a long running data retrieval operation is done. | |
authors: | |
- Per Fredelius | |
resources: | |
- http://no | |
normalize_css: no |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment