27. node.innerHTML = '<form method="post" action="/action">' +
'<p>' +
'<label>' +
'Username: <input type="text" name="username">' +
'</label>' +
'<label>' +
'Password: <input type="password" name="password">' +
'</label>' +
'</p>' +
'</form>';
var form = document.createElement('form');
var p = document.createElement('p');
form.setAttribute('action', '/login');
form.setAttribute('method', 'post');
var usernameLabel = document.createElement('label');
var usernameText = document.createTextElement('Username: ');
var usernameInput = document.createElement('input');
usernameInput.setAttribute('type', 'text');
usernameInput.setAttribute('name', 'username');
form.appendChild(p);
p.appendChild(usernameLabel);
// ... at this point I decided to give
// all this up and become a farmer
Tuesday, May 3, 2011
28. var html = {
element: function(name, attributes, children) {
var node = document.createElement(name);
for (var attr in attributes) {
node.setAttribute(attr, attributes[attr]);
}
for (var i=0, len=children.length; i < len; i++) {
node.appendChild(children[i]);
}
return node;
}
}
Tuesday, May 3, 2011
29. var form = html.element(
'form',
{ action: '/login', method: 'post' }
[
html.element('p', {}, [
html.element('label', {}, [
document.createTextElement('Username: '),
html.element('input', { type: 'text', name: 'username' }, []),
// ... you get the idea
])
])
]
);
Tuesday, May 3, 2011
30. var form = html.form({ action: '/login', method: 'post' },
[
html.p({}, [
html.label({}, [
document.createTextElement('Username: '),
html.input({ type: 'text', name: 'username' }, []),
// ... you still get the idea, right?
])
])
]
);
Tuesday, May 3, 2011
31. function elementBuilder(name) {
return function(attributes, children) {
return html.element(name, attributes, children);
}
}
function generateBuilderFunctions(elements) {
for (var i=0, len=elements.length; i < len; i++) {
html[elements[i]] = createElementBuilder(elements[i]);
}
}
generateBuilderFunctions("p|div|span|strong|em|img|table|tr|
td|th|thead|tbody|tfoot|pre|code|h1|h2|h3|h4|h5|h6|ul|ol|li|
form|input|textarea|legend|fieldset|select|option|
blockquote|cite|br|hr|dd|dl|dt|address|a|button|abbr|
acronym|script|link|style|bdo|ins|del|object|param|col|
colgroup|optgroup|caption|label|dfn|kbd|samp|var".split
("|"));
Tuesday, May 3, 2011
53. function elementBuilder(name) {
return function() {
var attributes = {}, children = [],
args = Array.prototype.slice.call(arguments);
// if the first arg is not a element or a string then its an attributes hash
if (!args[0].nodeType && typeof args[0] != 'string') {
attributes = args.unshift();
}
// children can be an array or remaining args
if (Array.isArray(args[0])) {
args = args[0];
}
// add rest of args as children converting any strings to text nodes
for (var i=0, len=args.length; i < len; i++) {
if (typeof args[i] == 'string') {
children.push(document.createTextNode(args[i]));
} else {
children.push(args[i]);
}
}
return html.element(name, attributes, children);
}
}
Tuesday, May 3, 2011
64. var lib = (function() {
var private = function() {
// you can't mess with me
};
return {
_internal: function() {
// you probably shouldn't mess with me
},
public: function() {
// I'm part of the API - call me sometime
}
}
}());
Tuesday, May 3, 2011