I'm trying to write more structured / object-oriented JS, as opposed to my old trusted "jQuery spaghetti" approach. Below I have a bit of JS to create multiple widgets on a page, and each widget has a "close" and "favorite" button.
My question is twofold:
1) Let's say we have 10-20 items/widgets on the page/dom (with a class of "item") that we want to apply JS functionality to. Is there a better way of creating a new instance of our "Item" class for each element than how I'm doing this at the very bottom of the code below?
2) Is there another way of accessing an object's properties and methods while inside a method? For example, something like "this.methodName" works in some instances, but in other instances the context of "this" will not be the object, so I'm not sure how to access things.
You'll see in my "events" method I use ".bind(this)" to alter the context of "this" so I can access object properties once I'm in the "close" or "favorite" methods. I'm thinking this use of "bind" isn't the best approach here - is there a more consistent / clean way of accessing object properties & methods without micromanaging "this" context?
var Item = function(el) {
// define variables we will need
this.elem = el;
this.closeButton = el.find(".close");
this.favoriteButton = el.find(".favorite");
// run ui event listeners when instance is created
this.events();
};
// ui event listeners - call appropriate method
Item.prototype.events = function() {
this.closeButton.on("click", this.close.bind(this));
this.favoriteButton.on("click", this.favorite.bind(this));
};
// close method
Item.prototype.close = function() {
this.elem.remove();
};
// favorite method
Item.prototype.favorite = function() {
this.favoriteButton.remove();
this.elem.append("Favorited!")
};
/* all elements with a class of item should
receive our Item class features */
$(".item").each(function() {
new Item($(this));
});
Basically, I'm wondering how to write well structured OO JS while interacting with multiple DOM elements. I've seen a lot of great "module pattern" guides online but a lot of these follow the "singleton" approach. Or I might just be misunderstanding everything :)
Any help is appreciated!
Not sure if this is helpful or not: but here is a JSFiddle of this example - I know we shouldn't rely on JSFiddle links for questions or answers because they aren't useful for SO archiving purposes, but I have the JS above for reference, hopefully this fiddle is just a bonus help.