Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping a DOM element inside a JavaScript object

I've noticed a common pattern in the JavaScript I've been writing and was wondering if there is already a pattern out there that defines something similar as best practice? Essentially, it's how to get a DOM element and wrap it inside / associate it with a JavaScript object. Take this example, where you need a filter in your web app. Your page looks like this:

<html>
<head></head>
<body>
    <div id="filter"></div>
</body>
</html>

You'd then wrap the element like so:

var myFilter = new Filter({
    elem: document.getElementById('filter'),
    prop: 'stacks-test',
    someCallback: function() {
        // specify a callback
    }
});

And the JavaScript (where spec is an object passed to the constructor):

var Filter = function(spec) {
    this.elem = spec.elem;
    this.prop = spec.prop;
    this.callback = spec.someCallback;
    this.bindEvents();
};

Filter.prototype.bindEvents = function() {
    var self = this;
    $(this.elem).click(function(e) {
        self.updateFeed();
    };
};

Filter.prototype.updateFeed = function() {
    this.prop; // 'stacks-test'
    this.callback();
    // ...
    // code to interact with other JavaScript objects
    // who in turn, update the document
};

What is this kind of approach called, and what are the best practices and caveats?

like image 321
Matty F Avatar asked Jan 20 '11 05:01

Matty F


People also ask

How do you wrap an element in JavaScript?

function wrap_single(el, wrapper) { el. parentNode. insertBefore(wrapper, el); wrapper. appendChild(el); } let divWrapper; let elementToWrap; elementToWrap = document.

What is wrap in JavaScript?

The wrap() method wraps specified HTML element(s) around each selected element.

What is DOM wrapper?

Wrapping an object refers to creating a new object that references the original, but providing additional functionality through the new, wrapper object. For example, rather than extending a DOM Element object with a cross-browser addClass function like this: var element = document.

Can JavaScript modify DOM?

The HTML DOM allows JavaScript to change the content of HTML elements.


1 Answers

You might be interested in Dojo's widget library, Dijit - if I'm understanding your question correctly, it essentially does what you're asking, and a whole lot more.

In Dijit, a widget essentially encapsulates a DOM node, its contents, any JavaScript that defines its behavior, and (imported separately) CSS to style its appearance.

Widgets have their own lifecycle, registry, and events (including many which simply map to DOM events on a node within the widget, e.g. myWidget.onClick could effectively call myWidget.domNode.onclick).

Widgets can (but don't have to) have their initial contents defined in a separate HTML template file, through which it's also possible to bind events on nodes within the template to widget methods, as well as set properties on the widget that reference particular nodes in the template.

I'm barely scratching the surface here. If you want to read more on this, you can start with these reference pages:

  • http://dojotoolkit.org/reference-guide/dijit/info.html
  • http://dojotoolkit.org/reference-guide/dijit/_Widget.html (the base that all widgets extend)
  • http://dojotoolkit.org/reference-guide/dijit/_Templated.html (RE the HTML templating)
  • http://dojotoolkit.org/reference-guide/quickstart/writingWidgets.html (useful information when starting to write your own widgets)
  • http://dojotoolkit.org/reference-guide/dijit/ (for a bunch more info)

All said, I don't know what you're ultimately aiming for, and maybe this is all a bit much for your purposes (considering I'm throwing an entire other library at you), but figured it might pique your interest at least.

like image 70
Ken Franqueiro Avatar answered Oct 20 '22 10:10

Ken Franqueiro