{"id":62,"date":"2015-12-03T07:51:28","date_gmt":"2015-12-03T02:21:28","guid":{"rendered":"http:\/\/madhurendra.com\/?p=62"},"modified":"2015-12-03T07:54:51","modified_gmt":"2015-12-03T02:24:51","slug":"javascript-domless-events","status":"publish","type":"post","link":"https:\/\/madhurendra.com\/javascript-domless-events\/","title":{"rendered":"Javascript Domless events"},"content":{"rendered":"

Using jQuery or nodejs makes you addicted to pubsub (publish\/subscribe) model.
\nNodejs has nattive support for event emitters but javascript doesn’t have. Events are only binded to a DOM, using which doesn’t make sense and its also adds to performance cost.<\/p>\n

To solve the problem i wrote a custom class which implements `on` and `emit` method, Yes some methods like `once` are missing.<\/p>\n

var tikEvent = function () {\r\n    this.listeners = {};\r\n\r\n};\r\ntikEvent.prototype.on = function (event, callback) {\r\n    if (!this.listeners.hasOwnProperty(event)) {\r\n        this.listeners[event] = [];\r\n    }\r\n    this.listeners[event].push(callback);\r\n};\r\n\r\ntikEvent.prototype.emit = function (event) {\r\n    if (this.listeners.hasOwnProperty(event)) {\r\n        var args = Array.prototype.slice.call(arguments);\r\n        args.shift();\r\n\r\n        for (var i = 0; i < this.listeners[event].length; ++i) {\r\n            try {\r\n                this.listeners[event][i].apply(null, args);\r\n            } catch (e) {\r\n               console.error(e);\r\n            }\r\n        }\r\n    }\r\n};\r\n\r\n\r\n\/\/Some test code.\r\n\r\nvat test = new tikEvent();\r\ntest.on('emitted',function(d){\r\n\r\nconsole.log(d);\r\n}\r\n\r\ntest.emit('emitted',\"test\");<\/pre>\n","protected":false},"excerpt":{"rendered":"

Using jQuery or nodejs makes you addicted to pubsub (publish\/subscribe) model. Nodejs has nattive support for event emitters but javascript doesn’t have. Events are only binded to a DOM, using which doesn’t make sense and its also adds to performance cost. To solve the problem i wrote a custom class which implements `on` and `emit` … <\/p>\n

Continue reading<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[13,8],"tags":[],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-javascript","category-snippets","item-wrap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pciGs2-10","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":4,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":66,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/62\/revisions\/66"}],"wp:attachment":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}