Dom-Wrapper
A DOM Wrapper that adds a facilities for use DOM in JavaScript.
It intents to propose a semi-template interface but near from native DOM adding a few sugars as addChild()
, or text()
and returning the engine in every function for chaining methods.
It's a small and powerful core with a plugins system that can extend almost every behaviour.
You can create new HTML tags, new methods and reusable modules with complex templates and logic.
Installing
npm install dom-wrapper
or copy the bundle/dom-wrapper.min.js to your project and then use window.DomWrapper
(if you're not using browserify similar tools, you do not know what you're missing: Why to use browserify? ).
A template example
'use strict';var d = ; //or var d = DomWrapper; if you don't use CommonJSvar references = {};var dom =d;documentbody;referencestitletext"My SUPER title"; //changing props after inserted{ //Some sync logic.var comments = ;comments;comments;comments;return comments;}
New text tag example
text.js
'use strict';{thiselement = document;}module {engine;};
main.js
var domWrapper = ; //or var d = DomWrapper; if you don't use CommonJSdomWrapper;
The text is in the core plugins, but you can make any behaviour in your tags only adding methods in this
in your function and saving the resulting dom in this.elements
.
New attr method example
In order achieve chaining methods call, every new method is encouraged to return this
.
The DOM element is accessible via this.element
'use strict';{ifargumentslength === 1 return get;ifargumentslength === 2 return set;return this;}{thiselement;return this;}{return thiselement;}module {engine;engine;engine;};
New simple module example
simpleForm.js
'use strict';var d = ; //or var d = DomWrapper; if you don't use CommonJS{thisreferences = {};var dom = dform'my super form';thiselement = dom;attachEvent;return this;}{thisreferencesbutton;}module {engine;};
And then:
main.js
var domWrapper = ; //or var d = DomWrapper; if you don't use CommonJSdomWrapper;
Specifying a different document variable
In the browser you can use document
for creating DOM
. In node or other engines you do not have a document
variable globally accesible.
You can use setDocument()
for suply a alternative implementor of DOM interface.
'use strict';var document = ;var d = ; //or var d = DomWrapper.setDocument(document); if you don't use CommonJS
.
operator.
Using tags directly as functions without dot JavaScript has the with
statement. Let's be clear: It's dangeorus and can prevent the engine to make certain optimizations.
Nevertheless, there is a few cases when the with
statement is justified. In a template system where HTML tags are mapped with functions in an object, I found that the with
statement can be helpful.
Note: The with
statement is forbidden with 'use strict'
.
var d = ; //or var d = DomWrapper; if you don't use CommonJSvar dom;withddom =;
Roadmap
- Use getters for save from use parentheses
- Add fragment tag
- Add event plugin