reaching properties from hasChanged - javascript

i am trying to call a function from lit element hasChanged method,but it gives undefined
static get properties() {
let dis=this;
return {
projeid:{type:Number},
_arr:{type:Array},
_input_obj: {type: Object},
_funksiyalar:{type:Object},
input_objectler_array:
{
type:Array,
hasChanged:function(newVal,oldVal){
console.log(dis._funksiyalar);
return true;
}
}
}
}
constructor() {
super();
this._arr = ["No:", "Aciqlama", "Vahid", "Miqdar", "Vahidin Qiymeti", "Toplam"];
this._input_obj=this._arr.reduce((o, key) => Object.assign(o, {[key]: ""},{detaylar:[]}), {});
this.input_objectler_array=[];
this._funksiyalar={
Objectler_Array_hasChanged(newVal) {
let event = new CustomEvent("array-updated", {
detail: {
array: newVal
}
});
this.dispatchEvent(event);
}
};
}
how could i reach a property or method from lit-element hasChanged method?

hasChanged is called immediately and synchronously on set, so in your constructor:
this.input_objectler_array=[];
// hasChanged fires, this._funksiyalar is undefined
this._funksiyalar={...
You should be able to work around this just by defining _funksiyalar first;
this._funksiyalar={...
this.input_objectler_array=[];
// hasChanged fires, this._funksiyalar is now set
Lit uses a Proxy to create property set methods, so it's as if you had something like:
_internalVal;
set input_objectler_array(v) {
const hasChanged = function(newVal,oldVal){
console.log(dis._funksiyalar);
return true;
};
if(hasChanged(v, this._internalVal)) {
this._internalVal = v;
this.requestUpdate(); // this queues up the change to render
}
}
List uses hasChanged to determine whether it needs to change the value, so it's a bad idea to hook side effects like events to it. You might do this if you wanted the event to be cancellable or able to change the value, but I'd recommend either your own custom set method or moving that logic outside of the render control entirely (maybe with a ReactiveController) if you want to do that.
Instead override updated to fire events after a value changes:
updated(changedProperties) {
if (changedProperties.has('input_objectler_array')) {
const event = new CustomEvent('array-updated', {
detail: {
// component has updated, changedProperties holds the old value
array: this.input_objectler_array;
}
});
this.dispatchEvent(event);
}
}
Now this updated fires asynchronously, once the update has happened, so your array-updated event will fire once the component is connected and rendered in the page rather than in the constructor. This will allow programmatic creation to work:
const ele = document.createElement('my-element'); // constructor event fires here
ele.addEventListener('array-updated', doSomething);
containerElement.append(ele); // updated event fires after this

Related

Is there a way I can dynamically bind a string and the text it outputs without using setInterval?

Is there a way I can dynamically bind a string and the text it outputs without using setInterval? I want it to be similar to Angular and Vue though I want to do this with vanilla JS. I want to be able to open the console and change the value at any time and see the change output on my element. Thank you in advance!
I think your only two options are:
A. Edit the element directly, e.g.
myPublicElemeVariable.innerText = 'Bla'
B. Use a setter (or Proxy):
obj = {
get str() { return this.myStr; }
set str(val) {
elem.innerText = val;
this.myStr = val;
}
}
C. Just use a function/method!
If you mean you want change to be event-driven, there is already a very simple event framework in javascript - the EventTarget class as demonstrated by this Code Sandbox
//define a watchable thing
class ValueTarget extends EventTarget {
constructor(value = "") {
super();
this.setValue(value);
}
getValue() {
return this._value;
}
setValue(value) {
this._value = value;
this.dispatchEvent(new CustomEvent("change", { detail: { value } }));
}
}
//get the page elements
const inputElement = document.querySelector("input");
const outputElement = document.querySelector("h1");
//create a watchable thing
const fieldTarget = new ValueTarget("");
//wire events that will change the watchable
inputElement.addEventListener("input", function (e) {
fieldTarget.setValue(e.target.value);
});
//receive notifications from the watchable
fieldTarget.addEventListener("change", (e) => {
outputElement.textContent = e.detail.value;
});
You may be as well to build your own given how simple it is - maintains a list of listeners and calls them when notified. My work recently needed such a thing which I knocked up in Typescript at https://github.com/cefn/lauf/blob/main/modules/lauf-store/src/core/watchable.ts#L3-L21 and would therefore be very easy to redo in javascript.

Gia: How to pass event object to handler via eventbus

I am learning how to work with Gia (for small web projects) and I cannot find out how to pass an event object from one component to an event handler of another component over Gia's eventbus.
Here's two basic components, communicating over the eventbus:
class navigation extends Component {
constructor(element) {
super(element);
//
// Define "Sub-components"
this.ref = {
navLinks: [],
};
}
mount() {
//
// Listen for clicks on nav.-links
for (let i = 0; i < this.ref.navLinks.length; i++) {
const link = this.ref.navLinks[i];
link.addEventListener("click", this.handleNavLinkClick.bind(this));
}
}
handleNavLinkClick(e) {
//
// Emit event
let clickedLink = e.target;
if (clickedLink.classList.contains("callHeader")) {
eventbus.emit("callingSubpageHeader");
}
}
}
class subpageHeader extends Component {
mount() {
//
// Listen for call from eventbus
eventbus.on(
"callingSubpageHeader",
this.handleEventBusCall_callHeader.bind(this)
);
}
//
// Eventbus handler(s)
handleEventBusCall_callHeader() {
console.log("The subpage-header was called.");
}
}
The emitting of the event and the subsequent call of the handler inside the second component works just fine. But I would like to pass additional information from the first to the second component when the handler is called. The Gia documentation mentions that the emit method of the eventbus can pass an eventObject to the handler:
Calls any handlers previously registered with the same event name.
Optional event object can be used as a argument, which gets passed
into a handlers as an argument.
eventbus.emit('eventName'[, eventObject]);
Unfortunately, there is no example and I don't know how passing the object works. I tried adding "something" (in this case the link that was clicked in the first component) to the call of the emit-function, but have no idea how/where I can read/use this nor if passing something as an eventObject works this way:
class navigation extends Component {
constructor(element) {
super(element);
//
// Define "Sub-components"
this.ref = {
navLinks: [],
};
}
mount() {
//
// Listen for clicks on nav.-links
for (let i = 0; i < this.ref.navLinks.length; i++) {
const link = this.ref.navLinks[i];
link.addEventListener("click", this.handleNavLinkClick.bind(this));
}
}
handleNavLinkClick(e) {
//
// Emit event
if (clickedLink.classList.contains("callHeader")) {
eventbus.emit("callingSubpageHeader", [e.target]);
}
}
}
It'd be great if someone could explain the concept and syntax of passing an eventObject in this scenario.
The event handler is passed the object from the event as a parameter, so your handler can grab that object as a variable from its function signature, like this:
handleEventBusCall_callHeader(target) {
console.log("The subpage-header was called.");
}
the variable target inside of your event handler is now equal to the object you passed with the event.
When you call the event, you don't need to put your argument in [], that will just put it into an array before passing it which will give you headaches later on. The brackets in the documentation just show that the second argument for eventbus.emit is optional.

Event handler not accessing the correct value from redux state

In my web application, I want to prompt user when he/she tries to close the browser/tab based upon Redux state using event handlers.
I am using the below code to prompt user before exiting based upon 'isLeaving' state.
function mapStateToProps(state) {
const {isLeaving} = state.app.getIn(['abc']);
return {
isLeaving
};
}
#connect(mapStateToProps, {}, undefined, {withRef: true})
export default class MyClass extends React.component {
#autobind
stayOnPage(event) {
if (this.props.isLeaving) {
const message = 'Are you sure you want to leave';
event.returnValue = message;
return message;
}
return false;
}
componentDidMount() {
window.addEventListener('beforeunload', (event) => {
this.stayOnPage(event);
});
}
componentWillUnmount() {
window.removeEventListener('beforeunload', (event) => {
this.stayOnPage(event);
});
}
componentWillReceiveProps(nextProps) {
if (this.props.prop1 !== nextProps.prop2) {
// do something
}
}
render() {
//
}
}
This code works fine. But whenever there is a change in prop1, I see that this.props.isLeaving does not have updated value.
Can somebody help? What is I'm doing wrong here?
You aren't cleaning up correctly in componentWillUnmount. The event handler you're trying to remove is a brand new function closure, not the same instance that you added. You should just attach the actual handler, rather than using an arrow function, like so:
componentDidMount() {
window.addEventListener('beforeunload', this.stayOnPage);
}
componentWillUnmount() {
window.removeEventListener('beforeunload', this.stayOnPage);
}
Possibly what you are seeing is the event triggering on a stale component instance, so it has old state.
React uses synthetic events, basically events are recycled to be more performant. You can read more on that here
What I normally do is pass the value I need on invocation
Not sure if this will work in your specific case because I define my event handlers in JSX instead of accessing the window object (which you might want to do as well but to be honest I'm not sure) but I use this pattern all the time to handle e.target.value properly

JS: Return value from EventListener function

i'm trying to return an array of instances of my Class. However the return is simply returning from the window.addEventListener('load', function(){} ); function I believe. As when I type plugins into the control it is undefined.
;class Parallax {
constructor(node) {
// Settings and defaults
// this.settings = {
// container: null,
// panels: [],
// defaultSpeed: 0.5
// };
}
static initialize() {
// Ensure DOM has loaded
window.addEventListener('load', function() {
// Does page contain any parallax panels?
let panels = document.querySelectorAll('[data-parallax]');
if (panels.length > 0) {
let instances = [];
// Parallax panels found, create instances of class and return them for reference
for (const panel in panels) {
if (panels.hasOwnProperty(panel)) {
instances.push(new this(panels[panel]));
}
}
return instances;
} else {
// Page doesn't appear to have Parallax
return false;
}
}.bind(this));
}
}
window.plugins = { "parallax-instances": Parallax.initialize() };
Not too sure how to go about doing this, I have tried assigning the window.addEventListener to a variable, however that doesn't return the value. I am retaining the class scope by binding this to my function call within the event listener.
Edit:
I did want to start the plugin by simply having the below code, however not too sure how to return possible multiple instances of the class as of course doing the below runs the constructor method first.
window.plugins = { "parallax-instances": new Parallax };
Edit 2
If I return the instances outside of the EventListener I get my desired result. However this could potentially return instances before the page has loaded right?
static initialize() {
this.instances = [];
// Ensure DOM has loaded
window.addEventListener('load', function() {
// Does page contain any parallax panels?
let panels = document.querySelectorAll('[data-parallax]');
if (panels.length > 0) {
// Parallax panels found, create instances of class and return them for reference
for (const panel in panels) {
if (panels.hasOwnProperty(panel)) {
this.instances.push(new this(panels[panel]));
}
}
}
}.bind(this));
return this.instances;
}
What I get in the console:
{parallax-instances: Array(2)}
parallax-instances:Array(2)
0:Parallax {}
1:Parallax {}
length:2
__proto__:Array(0)
__proto__:Object
An event listener function is a callback, and runs only when the event happens (in your case, on load).
Callbacks cannot return a value. You could use a variable in the outer scope (e.g. within your class), and reassign it within your event listener.
E.g.
constructor () {
this.instances = []
}
And then (instead of returning from your event listener):
this.instances = instances
Your initialize is not returning anything. If you elevate instances to outside your event listener and return that, you can remove the other return statements. Note that the returned array will be empty until the callback is actually called (after load).
static initialize() {
const instances = [];
window.addEventListener( "load", () => {
const panels = document.querySelectorAll( "[data-parallax]" );
for ( let i = 0; i < panels.length; i++ )
instances.push( new this( panels[ i ] ) );
});
return instances;
}
Since you would be returning an array right away, your false syntax would disappear and you'd test .length === 0 instead. You can get the behavior you specified using a getter instead.

addEventListener on custom object

I've created an object that has several methods. Some of these methods are asynchronous and thus I want to use events to be able to perform actions when the methods are done. To do this I tried to add the addEventListener to the object.
jsfiddle
var iSubmit = {
addEventListener: document.addEventListener || document.attachEvent,
dispatchEvent: document.dispatchEvent,
fireEvent: document.fireEvent,
//the method below is added for completeness, but is not causing the problem.
test: function(memo) {
var name = "test";
var event;
if (document.createEvent) {
event = document.createEvent("HTMLEvents");
event.initEvent(name, true, true);
} else {
event = document.createEventObject();
event.eventType = name;
}
event.eventName = name;
event.memo = memo || { };
if (document.createEvent) {
try {
document.dispatchEvent(event);
} catch (ex) {
iAlert.debug(ex, 'iPushError');
}
} else {
document.fireEvent("on" + event.eventType, event);
}
}
}
iSubmit.addEventListener("test", function(e) { console.log(e); }, false);
//This call is added to have a complete test. The errors are already triggered with the line before this one.
iSubmit.test();
This will return an error: Failed to add eventlisterens: TypeError: 'addEventListener' called on an object that does not implement interface EventTarget."
Now this code will be used in a phonegap app and when I do, it is working on android/ios. During testing, however, it would be nice if I could get it to work in at least a single browser.
PS> I know I could enable bubbling and then listen to the document root, but I would like to have just a little bit OOP where each object can work on its own.
addEventListener is intended for DOM Elements that implements certain event-related interfaces. If you want an event system on pure JavaScript objects, you are looking for a custom event system. An example would be Backbone.Events in Backbone.js. The basic idea is using an object as a hash to keep track of registered callbacks.
Personally I use this: emitter.
It's a fairly simple and elegant solution - with sweet short method names like on(), off() and emit(). you can either create new instances with new Emitter(), or use Emitter(obj) to mix event capabilities into existing objects. Note this library is written for use with a CommonJS module system, but you can use it anywhere else by removing the module.exports = ... line.
If you don't need true event features(such as bubbling, stopPropagation), then you can implement your own events. addEventListener is just an API of the DOM, so you don't really need it for your own objects outside the DOM. If you want to create an evented pattern around an object, here's a good way to do it that does not require any extra browser APIs and should be very backwards-compatible.
Let's say you have an object where you want a bunch of events to be triggered when the dispatch method is called:
var OurDispatcher, dispatcher;
OurDispatcher = (function() {
function OurDispatcher() {
this.dispatchHandlers = [];
}
OurDispatcher.prototype.on = function(eventName, handler) {
switch (eventName) {
case "dispatch":
return this.dispatchHandlers.push(handler);
case "somethingElse":
return alert('write something for this event :)');
}
};
OurDispatcher.prototype.dispatch = function() {
var handler, i, len, ref;
ref = this.dispatchHandlers;
for (i = 0, len = ref.length; i < len; i++) {
handler = ref[i];
setTimeout(handler, 0);
}
};
return OurDispatcher;
})();
dispatcher = new OurDispatcher();
dispatcher.on("dispatch", function() {
return document.body.innerHTML += "DISPATCHED</br>";
});
dispatcher.on("dispatch", function() {
return document.body.innerHTML += "DISPATCHED AGAIN</br>";
});
dispatcher.dispatch();
It really doesn't have to be more complicated than that, for the most part. This way you have some decent control over your events and you don't need to worry about backward-compatibility or external libraries because everything there is widely supported. Technically, you could even do without setTimeout and handle your callbacks without any APIs. Anything else like stopPropagation() would have to be handled yourself.
https://jsfiddle.net/ozsywxer/
There are, of course, polyfills for CustomEvent, but unless I need advanced event features, I prefer to wrap my own eventing system into a "class" and extending other classes/functions with it.
Here's the CoffeeScript version, which is what the JavaScript is derived from:
https://jsfiddle.net/vmkkbbxq/1/
^^ A bit easier to understand.
If you want to listen a javascript object you have three ways:
Use sub/pub pattern which has a lot of implementations in javascript
Or use native implementation via Object get/set operators, Object.defineProperty, Object.prototype.watch or Proxy API
Use Object.observe. Works Chrome 25+(Jan 2014). But became deprecated in 2016
About sup/pub pattern:
You need to publish events.
About native implementations:
Object get/set operators is enough to listen add, remove, change,
get events. Operators have good support. Problems only in IE8-.
But if you want to use get/set in IE8 use Object.defineProperty but
on DOM objects or use Object.defineProperty sham.
Object.prototype.watch has the good ES5 polyfill.
Proxy API needs ES Harmony support.
Object.observe example
var o = {};
Object.observe(o, function (changes) {
changes.forEach(function (change) {
// change.object contains changed object version
console.log('property:', change.name, 'type:', change.type);
});
});
o.x = 1 // property: x type: add
o.x = 2 // property: x type: update
delete o.x // property: x type: delete
There are two problems.
First, the iSubmit.addEventListener() method is actually a method on the EventTarget DOM interface:
EventTarget
EventTarget # addEventListener()
These are inteded for use only on DOM elements. By adding it to the iSubmit object as a method, you're calling it on an object that is not an EventTarget. This is why Chrome throws an Uncaught TypeError: Illegal invocation JavaScript error.
The first problem is critical, but if you could use EventTarget#addEventListener() your code would not work because the event is being added to iSubmit but dispatched from document. Generally, the same object's methods need to be used when attaching event listeners and dispatching events (unless you're using bubbling events, which is a different story - Note: bubbling is not restricted to JavaScript or DOM related events, for example).
Using custom events with your own objects is very normal. As Evan Yu mentioned, there are libraries for this. Here are a couple:
millermedeiros / js-signals
Wolfy87 / EventEmitter
I have used js-signals and like it quite a bit. I have never used Wolfy87/EventEmitter, but it has a nice look to it.
Your example might look something like the following if you used js-signals
jsFiddle
var iSubmit = {
finished: new signals.Signal(),
test: function test(memo) {
this.finished.dispatch(memo || {});
}
};
iSubmit.finished.add(function(data) {
console.log('finished:', data);
});
iSubmit.test('this is the finished data');
// alternatively
iSubmit.finished.dispatch('this is dispatched directly from the signal');
Just speculation; I haven't tried it myself. But you can create a dummy element and fire/listen to events on the dummy element.
Also, I prefer going without libraries.
function myObject(){
//create "dummy" element
var dummy = document.createElement('dummy');
//method for listening for events
this.on = function(event, func){dummy.addEventListener(event, func);};
//you need a way to fire events
this.fireEvent = function(event, obj){
dummy.dispatchEvent(new CustomEvent(event, {detail: obj}));
}
}
//now you can use the methods in the object constructor
var obj = new myObject();
obj.on("custom", function(e){console.log(e.detail.result)});
obj.fireEvent("custom", {result: "hello world!!!"});
Here's a simple event emitter:
class EventEmitter {
on(name, callback) {
var callbacks = this[name];
if (!callbacks) this[name] = [callback];
else callbacks.push(callback);
}
dispatch(name, event) {
var callbacks = this[name];
if (callbacks) callbacks.forEach(callback => callback(event));
}
}
Usage:
var emitter = new EventEmitter();
emitter.on('test', event => {
console.log(event);
});
emitter.dispatch('test', 'hello world');
If you are in a Node.js environment then you can use Node's EventEmitter class:
CustomObject.js
const EventEmitter = require('events');
class CustomObject extends EventEmitter {
constructor() {
super();
}
doSomething() {
const event = {message: 'Hello World!'};
this.emit('myEventName', event);
}
}
module.exports = CustomObject;
Usage:
const CustomObject = require('./CustomObject');
// 1. Create a new instance
const myObject = new CustomObject();
// 2. Subscribe to events with ID "myEventName"
myObject.on('myEventName', function(event) {
console.log('Received event', event);
});
// 3. Trigger the event emitter
myObject.doSomething();
If you want to use Node's EventEmitter outside of a Node.js environment, then you can use webpack (preferably v2.2 or later) to get a bundle of your CustomClass together with an EventEmitter polyfill (built by webpack).
Here is how it works (assuming that you installed webpack globally using npm install -g webpack):
Run webpack CustomObject.js bundle.js --output-library=CustomObject
Include bundle.js in your HTML page (it will expose window.CustomObject)
There's no step three!
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Title</title>
<script src="bundle.js"></script>
</head>
<body>
<script>
// 1. Create a new instance
const myObject = new window.CustomObject();
// 2. Subscribe to events with ID "myEventName"
myObject.on('myEventName', function(event) {
console.log('Received event', event);
});
// 3. Trigger the event emitter
myObject.doSomething();
</script>
</body>
</html>
I have been able to achieve this by wrapping an element in javascript class.
Important point is that the element does not have to exist in dom. Also, the element tag name can be anything such as the custom class name.
'''
class MyClass
{
constructor(options )
{
this.el = document.createElement("MyClass");//dummy element to manage events.
this.el.obj= this; //So that it is accessible via event.target.obj
}
addEventListener()
{
this.el.addEventListener(arguments[0],arguments[1]);
}
raiseEvent()
{
//call this function or write code below when the event needs to be raised.
var event = new Event('dataFound');
event.data = messageData;
this.el.dispatchEvent(event);
}
}
let obj = new MyClass();
obj.addEventListener('dataFound',onDataFound);
function onDataFound()
{
console.log('onDataFound Handler called');
}
'''
This article explains creating custom events: http://www.sitepoint.com/javascript-custom-events/
here is an example:
create the event -
var event = new CustomEvent(
"newMessage",
{
detail: {
message: "Hello World!",
time: new Date(),
},
bubbles: true,
cancelable: true
}
);
assign the event to something -
document.getElementById("msgbox").dispatchEvent(event);
subscribe to the event -
document.addEventListener("newMessage", newMessageHandler, false);
Usage: jsfiddle
This is a naive approach but might work for some applications:
CustomEventTarget.prototype = {
'constructor': CustomEventTarget,
on: function( ev, el ) { this.eventTarget.addEventListener( ev, el ) },
off: function( ev, el ) { this.eventTarget.removeEventListener( ev, el ) },
emit: function( ev ) { this.eventTarget.dispatchEvent( ev ) }
}
function CustomEventTarget() { this.eventTarget = new EventTarget }
I think you can use Object $Deferred and promises.
It'll let you do something like this:
Stacked: bind multiple handlers anywhere in the application to the same promise event.
var request = $.ajax(url);
request.done(function () {
console.log('Request completed');
});
// Somewhere else in the application
request.done(function (retrievedData) {
$('#contentPlaceholder').html(retrievedData);
});
Parallel tasks: ask multiple promises to return a promise which alerts of their mutual completion.
$.when(taskOne, taskTwo).done(function () {
console.log('taskOne and taskTwo are finished');
});
Sequential tasks: execute tasks in sequential order.
var step1, step2, url;
url = 'http://fiddle.jshell.net';
step1 = $.ajax(url);
step2 = step1.then(
function (data) {
var def = new $.Deferred();
setTimeout(function () {
console.log('Request completed');
def.resolve();
},2000);
return def.promise();
},
function (err) {
console.log('Step1 failed: Ajax request');
}
);
step2.done(function () {
console.log('Sequence completed')
setTimeout("console.log('end')",1000);
});
Source here:
http://blog.mediumequalsmessage.com/promise-deferred-objects-in-javascript-pt2-practical-use
Here is how you do this with Node.js style syntax in the browser.
The Events class:
stores callbacks in a hash associated with event keys
triggers the callbacks with the provided parameters
To add the behavior to your own custom classes just extend the Events object (example below).
class Events {
constructor () {
this._callbacks = {}
}
on (key, callback) {
// create an empty array for the event key
if (this._callbacks[key] === undefined) { this._callbacks[key] = [] }
// save the callback in the array for the event key
this._callbacks[key].push(callback)
}
emit (key, ...params) {
// if the key exists
if (this._callbacks[key] !== undefined) {
// iterate through the callbacks for the event key
for (let i=0; i<this._callbacks[key].length; i++) {
// trigger the callbacks with all provided params
this._callbacks[key][i](...params)
}
}
}
}
// EXAMPLE USAGE
class Thing extends Events {
constructor () {
super()
setInterval(() => {
this.emit('hello', 'world')
}, 1000)
}
}
const thing = new Thing()
thing.on('hello', (data) => {
console.log(`hello ${data}`)
})
Here is a link a github gist with this code: https://gist.github.com/alextaujenis/0dc81cf4d56513657f685a22bf74893d
For anyone that's looking for an easy answer that works.
I visited this document, only to learn that most browser doesn't support it.
But at the bottom of the page, there was a link to this GitHub page that basically does what the Object.watch() and Object.unwatch() would have done, and it works for me!
Here's how you can watch for changes
/*
* object.watch polyfill
*
* 2012-04-03
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
* https://gist.github.com/eligrey/384583
*/
// object.watch
if (!Object.prototype.watch) {
Object.defineProperty(Object.prototype, "watch", {
enumerable: false
, configurable: true
, writable: false
, value: function (prop, handler) {
var
oldval = this[prop]
, newval = oldval
, getter = function () {
return newval;
}
, setter = function (val) {
oldval = newval;
return newval = handler.call(this, prop, oldval, val);
}
;
if (delete this[prop]) { // can't watch constants
Object.defineProperty(this, prop, {
get: getter
, set: setter
, enumerable: true
, configurable: true
});
}
}
});
}
// object.unwatch
if (!Object.prototype.unwatch) {
Object.defineProperty(Object.prototype, "unwatch", {
enumerable: false
, configurable: true
, writable: false
, value: function (prop) {
var val = this[prop];
delete this[prop]; // remove accessors
this[prop] = val;
}
});
}
And this should be your code:
var object = {
value: null,
changeValue: function(newValue) {
this.value = newValue;
},
onChange: function(callback) {
this.watch('value', function(obj, oldVal, newVal) {
// obj will return the object that received a change
// oldVal is the old value from the object
// newVal is the new value from the object
callback();
console.log("Object "+obj+"'s value got updated from '"+oldValue+"' to '"+newValue+"'");
// object.changeValue("hello world");
// returns "Object object.value's value got updated from 'null' to 'hello world'";
// and if you want the function to stop checking for
// changes you can always unwatch it with:
this.unwatch('value');
// you can retrieve information such as old value, new value
// and the object with the .watch() method, learn more here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch
})
}
};
or as short as:
var object = { user: null };
// add a watch to 'user' value from object
object.watch('user', function() {
// object user value changed
});
Use the createElement to create a dummy element.
typescript
class Person {
name: string
el: HTMLElement // event listener
constructor(name: string) {
this.name = name
this.el = document.createElement("Person"); // dummy element to manage events
(this.el as any).object = this // set dummy attribute. (Optional) So that it is accessible via `event.target.object`
}
AddEventListener(type: string, listener: any) {
this.el.addEventListener(type, listener)
}
DispatchEvent(type: string, data: any = null) {
const event = new Event(type);
(event as any).data = data //dummy attribute (Optional)
this.el.dispatchEvent(event)
}
}
const carson = new Person("Carson")
carson.AddEventListener("Say", (e: Event) => {
const person = (e.target as any).object as Person // get dummy attribute
const data = (e as any).data // get dummy attribute
if (data !== undefined && data.stopImmediatePropagation === true) {
e.stopImmediatePropagation()
}
console.log(`${person.name}`, data)
})
carson.AddEventListener("Say", () => {
console.log("Say2")
})
carson.DispatchEvent("Say")
// Output:
// Carson undefined
// Say2
carson.DispatchEvent("Say", "hello world!")
// Carson hello world!
// Say2
carson.DispatchEvent("Say", {stopImmediatePropagation: true})
// Carson {stopImmediatePropagation: true}
Runnable Example
<script>
class Person {
constructor(name) {
this.name = name
this.el = document.createElement("Person") // dummy element to manage events
this.el.object = this // set dummy attribute. (Optional) So that it is accessible via `event.target.object`
}
AddEventListener(type, listener) {
this.el.addEventListener(type, listener)
}
DispatchEvent(type, data) {
const event = new Event(type)
event.data = data // set dummy attribute
this.el.dispatchEvent(event)
}
}
const carson = new Person("Carson")
carson.AddEventListener("Say", (e) => {
const person = e.target.object // get dummy attribute
const data = e.data // get dummy attribute
if (data !== undefined && data.stopImmediatePropagation === true) {
e.stopImmediatePropagation()
}
console.log(`${person.name}`, data)
})
carson.AddEventListener("Say", (e) => {
console.log("Say2")
})
carson.DispatchEvent("Say")
carson.DispatchEvent("Say", "hello world!")
carson.DispatchEvent("Say", {stopImmediatePropagation: true})
</script>
With ES6 class, object & callbacks you can create your own custom event system with the following code:
class ClassWithEvent {
//Register a new event for the class
RegisterEvent(event,Handler){
var eventName = `event_on${event}`;
if(this.hasOwnProperty(eventName) == false){
this[eventName] = [];
}
this[eventName].push(Handler);
}
//private unregister the event
#unregisterEvent(event){
var eventName = `event_on${event}`;
delete this[eventName];
}
//raise event
#dispatchEvent(name, event) {
var eventName = `event_on${name}`;
if (this.hasOwnProperty(eventName))
this[eventName].forEach(callback => callback(event));
}
//public method
sayhello(name){
this.#dispatchEvent("beforehello",{'name':name,'method':'sayhello'});
alert(`Hello ${name}`);
this.#dispatchEvent("afterhello",{'name':name,'method':'sayhello'});
}
}//EOC
Once defined you can call it as:
var ev = new ClassWithEvent();
ev.RegisterEvent("beforehello",(x)=> console.log(`Event:before ${x.name} ${x.method} oh`));
ev.RegisterEvent("afterhello",(x)=> console.log(`Event:after ${x.name} ${x.method} oh`));
ev.RegisterEvent("beforehello",(x)=> console.log(`Event2:before ${x.name} ${x.method} oh`));
ev.sayhello("vinod");
So in the code above we have registered 3 events handlers which will be invoked by #dispatchEvent() when we call the sayhello() method.
The instance of the class will look something like this:
We can see in the image above the onbeforehello event has two handlers and it will be invoke in the sequence it is defined.

Categories