OO Javascript / jQuery event click handler - javascript

I'm new on JS developing, I'm trying to use OO Javascript using "classes", I have a class named "Timeblock", with it constructor:
var Timeblock = function(occupied, on, element) {
this.occupied = occupied;
this.on = on;
this.room_id = 0;
this.element= element;
}
I've created an object this way:
timeblock = new Timeblock(false, false, $('#element'));
Everything works at this point, now I'm trying to add a click event listener to the class constructor on its element var, I tried:
var Timeblock = function(occupied, on, element) {
this.occupied = occupied;
this.on = on;
this.room_id = 0;
this.element = element;
timeblock = this;
this.element.click(function() {
timeblock.update();
});
}
Timeblock.prototype.update = function() {
if (!occupied) {
this.element.addClass('on');
}
}
When debugging in Chrome, I put a breakpoint on this.element.addClass('on'); inspecting the object I can read this.element: jQuery.fn.init[0] and I can't get it to work.
Even in console when I type this.element I get undefined, but if I type this.occupiedI get false
My question is, why I'm getting undefined? how can I make it work?, I've searched and searched but can't find any good material to study OO Javascript.

var Timeblock = function(occupied, on, element) {
this.occupied = occupied;
this.on = on;
this.room_id = 0;
this.element = element;
var timeblock = this; // need var statement
this.element.click(function() {
timeblock.update();
});
}
Timeblock.prototype.update = function() {
if (!this.occupied) { // forgot this
this.element.addClass('on');
}
}
Two errors. I fixed them, and left comments explaining.

Related

javascript class with event methods

My goal is to make a class with some chained functions, but I'm stuck and hoping for some help. This is what I got:
robin = new batman("myiv");
var batman = (function() {
var me = this;
function batman(id){
me._id=id;
document.getElementById(id).addEventListener('mousemove', me.mouseMoving.bind(me),true);
}
this.mouseMoving = function(){
document.getElementById(me._id).style.background="orange";
}
return batman;
}
And this pseudo code is what I am aiming to get. Basically, pass in the ID of an element in my HTML and chain functions to it such as onclick etc, and whatever code inside there, runs. as in example, changing background colors.
Is it possible?
superman("mydiv"){
.onmouseover(){
document.getElementById(the_id).style.background="#ffffff";
},
.onmouseout(){
document.getElementById(the_id).style.background="#000000";
},
etc...
}
edit: updated with missing code: "return batman;"
You can do method chaining by returning the current object using this keyword
var YourClass = function () {
this.items = [];
this.push = function (item) {
if (arguments) {
this.items.push(item);
}
return this;
}
this.count = function () {
return this.items.length;
}
}
var obj = new YourClass();
obj.push(1).push(1);
console.log(obj.count())
Working sample
https://stackblitz.com/edit/method-chaining-example?file=index.js

Linking property of a custom element to it's attribute

Question & Demo
I've recently started to work with custom elements.
As you know, a HTMLElement has both a markup inside the document, and a JavaScript object. So, with my custom element, I've tried to link the JavaScript object properties with the element's attributes.
So, if any of those is updated, the other would be updated as well. But this isn't happening and I swear I've tried everything, maybe is something stupid I'm missing but for me, how this code is behaving is a freaking mistery.
After reading the code explanation below and seen the demo, you should be able to understand my question:
Why are the custom element attributes updating correctly, but not it's properties?
I've setup a JSFiddle to illustrate my problem, and I will be going over how the code is supposed to work in this post.
HTML
<e-button color="red" width="250px">RED BUTTON</e-button>
Well it rarely gets any simpler than that. I create a custom object called "e-button", with color=red and width=250px.
JavaScript
var eButtonProto = Object.create(HTMLElement.prototype);
eButtonProto.createdCallback = function() {
this.__htmlToJsProp(); //Gets all the HTML attributes and makes them accessible via JS.
this.__processAttr(); //Makes decision upon predefined attributes.
}
eButtonProto.__htmlToJsProp = function() {
var attr = this.attributes;
for (var i = 0; i < attr.length; i++) {
var current = attr[i];
var name = current.name;
var value = current.value;
this[name] = value;
Object.defineProperty(this, name, {
get: function() {
return this.getAttribute(name);
},
set: function(val) {
this.setAttribute(name, val);
}
});
}
}
eButtonProto.attributeChangedCallback = function(name, oldVal, val) {
this[name] = val;
this.__processAttr();
}
eButtonProto.__processAttr = function() {
var color = this.color || this.defaults.color;
this.style.backgroundColor = color;
}
eButtonProto.defaults = {
color: "whitesmoke"
}
var eButton = document.registerElement("e-button", {
prototype: eButtonProto
});
window.onload = function() {
redButton = document.querySelector("e-button[color=red]");
console.log("button ATTRIBUTES", redButton.getAttribute("color"), redButton.getAttribute("width"));
console.log("button PROPERTIES", redButton.color, redButton.width);
} < /script>
The really important code snippets here are these, which essentialy should make my idea work, first, the __htmlToJsProp() function:
eButtonProto.__htmlToJsProp = function() {
var attr = this.attributes; //Gets the element's attributes.
for (var i = 0; i < attr.length; i++) {
var current = attr[i]; //Element attribute name,value pair.
var name = current.name; //Attribute name.
var value = current.value; //Attribute value.
Object.defineProperty(this, name, { //Defines the element property from the attribute name, for simplicity I will be using the color attribute as my example.
get: function() {
return this.getAttribute(name); //When accessing element.color you should get element.getAttribute("color")
},
set: function(val) {
this.setAttribute(name, val); //When setting element.color = "red" you should also be doing element.setAttribute("color","red");
}
});
this[name] = value; //Sets element.color = "red"
}
}
and then the attributeChangedCallback function:
eButtonProto.attributeChangedCallback = function(name, oldVal, val) {
this[name] = val; //This would be the other way around, if the attribute is updated via setAttribute, or the browser console, the property is updated (works).
this.__processAttr(); //You can ignore this
}
Conclusions
You see after testing A LOT I found that if you place yourself in the for loop and output the property value, it will give you element.color = "red" and element.width = "250px";
But if you test it outside the for loop, it gives you element.color = "250px" and element.width = "250px" for the properties but the attributes update properly, that is element.getAttribute("color") = "red" and element.getAttribute("width") = "250px".
If you made it this far, well thanks, hopefully you can find a way out of this problem, which I really don't seem to be able to solve, happy coding :)
Your issue seems to be within the for loop, the getters and setters are called later, so the value of i isn't what you think it is, the loop completes and sets i to the latest iterated value.
You'll solve it with a closure
eButtonProto.__htmlToJsProp = function () {
var attr = this.attributes;
for (var i = 0; i < attr.length; i++) {
(function(current, self) {
var name = current.name;
var value = current.value;
Object.defineProperty(self, name, {
get: function () {
return this.getAttribute(name);
},
set: function (val) {
this.setAttribute(name, val);
}
});
self[name] = value;
})(attr[i], this);
}
}
FIDDLE

using revealling moduler pattern for complex in javascript

I have a very complex class so i decided to break into sub modules and trying to use revealing modules pattern.
I have main class and decided to divide into smaller container function. but in current scenario
But i am not able to access any internal function from outside i.e callSearchResultWithCallBack using searchFinder.Search.callSearchResultWithCallBack(). which pattern should i use to keep this code clean as well have control to call internal function in sub module.
Thanks
var searchFinder;
function SearchFinder() {
me = this;
this.searchResult = null;
this.init = function() {
declareControls();
createAccordian();
addEvents();
fillControls();
var declareControls = function() {
this.SearchButtons = jQuery('.doSearch');
this.InputLocation = jQuery('#inputLocation');
this.InputDistanceWithIn = jQuery('#inputDistanceWithIn');
this.InputName = jQuery('#inputName');
}
var addEvents = function() {
me.SearchButtons.click(function() {
me.Search();
});
}
var fillControls = function() {
var getGetCategory = function() {
}
}
}
this.Search = function() {
var url = '';
var searchCriteria = {};
validateAndCreateCriteria();
callSearchResultWithCallBack();
function validateAndCreateCriteria() {
function validateAandGetCategory() {
if (SearchValidation.ValidateZipCode(me.InputLocation.val().trim())) {
searchCriteria.location = me.InputLocation.val().trim();
} else if (SearchValidation.ValidateCityState(me.InputLocation.val().trim())) {
searchCriteria.location = me.InputLocation.val().trim();
}
}
}
// need to access it outsite
function callSearchResultWithCallBack() {
me.searchResult(searchCriteria, SearchResultCallBack);
function SearchResultCallBack() {
}
}
}
}
jQuery(function() {
searchFinder = new SearchFinder();
searchFinder.init();
searchFinder.Search.callSearchResultWithCallBack();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
This code has multiple issues, first I will address the fact that for example declareControls is not executing. First declare the function than execute!
this.init = function() {
var declareControls = function() {
this.SearchButtons = jQuery('.doSearch');
this.InputLocation = jQuery('#inputLocation');
this.InputDistanceWithIn = jQuery('#inputDistanceWithIn');
this.InputName = jQuery('#inputName');
}
var addEvents = function() {
this.SearchButtons.click(function() {
me.Search();
});
}
var fillControls = function() {
var getGetCategory = function() {
}
}
declareControls();
//createAccordian(); //not defined
addEvents();
fillControls();
}
Now let's look at others problems that will arise.
the me object referring to this is in the scope of searchFinder and does not refer to the same this in the instance of searchFinder.
function jQuery can be replaced by the commonly used $.
searchFinder.Search.callSearchResultWithCallBack() this is never going to work. Since the Search function is an object and callSearchResultWithCallBack isn't a property of this function.
Solution; make it part of the prototype of Search.
Steps:
Move callSearchResultWithCallBack outside the search function.
Add prototype to Search function
Call function via prototype.
function callSearchResultWithCallBack() {
me.searchResult(searchCriteria, SearchResultCallBack);
function SearchResultCallBack() {
}
}
this.Search.prototype.callSearchResultWithCallBack = callSearchResultWithCallBack;
If you want to fire this function outside of search use this:
searchFinder.Search.prototype.callSearchResultWithCallBack();
Please remember that callSearchResultWithCallBack will throw an error because searchCriteria is undefined.
This fixes your problems for now, but this code has to be revised thoroughly. But this should get you started. http://ejohn.org/blog/simple-javascript-inheritance/

Jquery Referencing this

Given the object:
// A data set
$.DataArea = function () {
// Default options
$.extend(this, {
class: 'DataSet',
data: new Array(),
container: null
});
// Add a bar to this object
this.addBar = function(startDate, endDate, label) {
var insertPos = this.data.length;
this.data[insertPos] = new $.DataBar();
this.data[insertPos].startDate = startDate;
this.data[insertPos].endDate = endDate;
this.data[insertPos].label = label;
this.container.children('.jobArea').append('<div class="bar-wrapper"><div class="bar">' + label + '</div></div>');
}
// Bind the bar to a div
this.bind = function(docID) {
this.container = $('#' + docID);
this.container.append('<div class="jobArea"></div>')
};
this.init = function() {
this.container.children('.jobArea .bar, .jobArea .marker').each(function(i) {
$(i).bind("selectstart", _preventDefault);
});
};
};
The line $(this).bind("selectstart", _preventDefault); I think is not working, because $(this) is conflicting with the this of the object?
How can I correctly reference the selected element in the each loop in a non conflicting way? (If that's the problem)
Edit
DataArea in use:
var MyData = new $.DataArea();
MyData.bind("container");
MyData.addBar("", "", "Bar 1");
MyData.addBar("", "", "Bar 2");
MyData.init();
Go back to using this instead of i, and use the find()[docs] method instead of the children()[docs] method.
this.init = function() {
//------------v
this.container.find('.jobArea .bar, .jobArea .marker').each(function(i) {
$(this).bind("selectstart", _preventDefault);
});
};
This is necessary becuase .bar and .marker are not direct descendants of container.
The second variable passed to the .each() callback is the actual element. You should be able to re-write it like so:
this.container.children('.jobArea .bar, .jobArea .marker').each(function(i,e) {
$(e).bind("selectstart", _preventDefault);
});
Edit
I think it's also worth mentioning that the selectstart event is not supported in all browsers which may actually be the problem.

JavaScript Starter Framework Errors

I am trying to learn more about JavaScript and I am messing around with... stuff. Could someone tell me why I am getting errors in my script please.
// Effects object
var effects = {
// Display an object
show : function(obj) {
obj.style.display = 'block';
},
// Hide an object
hide : function(obj) {
obj.style.display = 'hide';
},
// Toggle
toggle : function(obj) {
if (obj instanceof Array) {
alert('array');
} else {
alert('single');
}
}
}
// Selector Class
var s = {
id : function(name) {
return document.getElementById(name);
},
class : function(name) {
node = document.getElementsByTagName("body")[0];
var a = [];
var re = new RegExp('\\b' + classname + '\\b');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))
a.push(els[i]);
return a;
}
}
window.onload = function() {
s.id('toggle-content').onClick(function() {
effects.toggle(s.class('hidden-content'));
});
}
I get the following errors:
When page loads:
Error: s.id("toggle-content").onClick
is not a function Source File:
http://localhost/cms/web/js/admin.js
Line: 40
On a slight tangent. Is there a standardised way to get a class from the dom?
Solved.
onclick= instead of onClick()
Event names in Javascript don't follow the camelCase style of properties and methods. They're all lower case. Change onClick to onclick and you should be good to go.
On your slight tangent, some browsers already support getElementsByClassName (which I think is what you're asking for). There are alternatives for browsers that don't support it.

Categories

Resources