I have the library that I have found to be very useful when creating dynamic content rich web applications. I call it DO.js. It allows me to create entire DOM structures inside of Javascript in a similar manner that HTML markup should look. Additionally, it does it all without writing any HTML strings. It uses a hierarchy of embedded functions and objects to create the DOM structure. Please see my site below for examples.
My question is, is this a good way of doing this? I can and I have (the website below is created in this fashion) created whole websites using this method. I made it so that I could easily load and display large amounts of data pulled via ajax and where each bit of data will have events and variables attached. Of course, this is hard and tacky to do using normal HTML.
I kinda created documentation for it. However, I am not very good with documentation(or writing in general.)
What should/could be done better? What would be the best programming practices in this case? Anykind of critique would be awesome.
Thanks
http://hurdevan.com/#!Home/Projects/DO.js
Please note that the website does not work at all in IE due to a Hashbang.js bug. And, I don't care about IE.
Code Examples:
Simple Example
with(DO.dom)
{
div({parentElement:document.body},
span(b('Hello World!'))
);
}
An example using events and such:
var win = {};
with(DO.dom)
{
win.root = div({parentElement:document.body,style:'border:1px solid black;width:200px;height:200px;position:fixed;'},
win.bar = div({style:'height:20px;text-align:center;background-color:#333333;color:white;cursor:pointer;',
onmousedown:DO.Func(true,function(eve,winobj)
{
window.onmousemove = winobj.bar.onmousemove;
window.onmouseup = winobj.bar.onmouseup;
winobj.offsetX = winobj.root.offsetLeft - eve.x;
winobj.offsetY = winobj.root.offsetTop - eve.y;
winobj.activeDrag = true;
},win),
onmouseup:DO.Func(true,function(eve,winobj)
{
window.onmousemove = false;
window.onmouseup = false;
winobj.activeDrag = false;
},win),
onmousemove:DO.Func(true,function(eve,winobj)
{
if(!winobj.activeDrag)return false;
winobj.root.style.position="fixed";
winobj.root.style.left = (eve.x + winobj.offsetX)+"px";
winobj.root.style.top = (eve.y + winobj.offsetY)+"px";
},win)
},'Move Window'),
win.body = div(
div(b('First Name:'),
win.firstName = input({type:'text'})
),
div(b('Last Name:'),
win.lastName = input({type:'text'})
),
div(b('Email:'),
win.email = input({type:'text'})
),
input({type:'submit',onclick:DO.Func(function(winobj)
{
alert("First Name:"+winobj.firstName.value +"\n"+"Last Name:"+winobj.lastName.value +"\n"+"Email:"+winobj.email.value +"\n");
},win)}),
"This window was created with DO.dom"
)
);
}
Another example that allows the DOM structure to be child/parent aware.
with(DO.dom)
{
var foo =div({parentElement:document.body},
div({nid:'inputElements';},
input({nid:'firstName'}),
input({nid:'lastName'}),
input({nid:'email'})
)
);
}
foo.inputElements.firstName.value = 'Hello';
foo.inputElements.lastName.value = 'Word';
foo.inputElements.email.value = '[email protected]';