{"id":557,"date":"2014-07-09T11:56:06","date_gmt":"2014-07-09T11:56:06","guid":{"rendered":"http:\/\/www.siriinnovations.com\/blog\/?p=557"},"modified":"2019-04-10T15:03:08","modified_gmt":"2019-04-10T09:33:08","slug":"decoupling-custom-jquery-events","status":"publish","type":"post","link":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/","title":{"rendered":"Decoupling with custom jQuery events"},"content":{"rendered":"<p><strong>Introduction:<\/strong><\/p>\n<p>Decoupling is when we remove these unnecessary relationships, allowing units of code to function independently. Applications should be able to function independently or with other apps in a container (think widgets).<\/p>\n<p>While the concept of\u00a0<a href=\"http:\/\/api.jquery.com\/trigger\/\">custom events in jQuery<\/a>\u00a0may not be a new one, I tend to only see them used amongst plugins, mainly to keep their events namespaced for fear of collusion with other events.<\/p>\n<p>The truth is they can be leveraged in everyday and jQuery code to help make life a little easier.<\/p>\n<p><strong>The problem<\/strong><\/p>\n<p>It\u2019s not uncommon to see a single event handler finding itself responsible for many things:<\/p>\n<div>\n<pre class=\"code\" lang=\"js\">$('.add-items').on('click', function(event) {\r\n$('.loading-message').show();\r\n$.ajax({\r\n\/\/ etc\r\n});\r\n$('.something-else').addClass('foo')\r\nevent.preventDefault();\r\n});<\/pre>\n<\/div>\n<p>Having an event handler responsible for a lot of different tasks will typically result in large, incoherent event callbacks. These can often get worse when other developers have to add in additional functionality later.<\/p>\n<p><strong>Decoupling all the things<\/strong><\/p>\n<p>One route I like to take is to separate the logic relating to the DOM event.This means just handling anything related to the event (preventDefault\u00a0or stopPropagation\u00a0calls, for example) and then firing a custom event and allowing another part of the application handle the \u2018business\u2019 side of things:<\/p>\n<div>\n<pre class=\"code\" lang=\"js\">var doc = $(document);\r\n$('.add-items').on('click', function(event) {\r\ndoc.trigger('addItem', [$(this)]);\r\nevent.preventDefault();\r\n});<\/pre>\n<\/div>\n<p>The first thing to note is that I\u2019m grabbing a reference to the document wrapped as a jQuery object:<\/p>\n<div>\n<pre class=\"code\" lang=\"js\">var doc = $(document);<\/pre>\n<\/div>\n<p>By triggering the events on this global object, it can become a sort of mediator between other parts of code.<\/p>\n<p>Storing a reference to it outside of the event handler callback also means that it won\u2019t be created each time the button is clicked. This is a good practice to get in the habit of.<\/p>\n<p>Then all that is required is to trigger an event of our choice and pass any useful data along with it.<\/p>\n<div>\n<pre class=\"code\" lang=\"js\">doc.trigger('addItem', [$(this)]);<\/pre>\n<\/div>\n<p>An object or array can be passed along with the event.\u00a0In this case i am passing along a jQuery object that refers to the clicked button.<\/p>\n<p><strong>Responding to the event<\/strong><\/p>\n<p>Listening for the\u00a0addItem\u00a0event is as simple as creating an event listener on the\u00a0documentobject:<\/p>\n<div>\n<pre class=\"code\" lang=\"js\">$(document).on('addItem', function(event, btnElement) {\r\n$('.something-else').addClass('foo')\r\n});\r\n$(document).on('addItem', function(event, btnElement) {\r\n$.ajax({\r\n\/\/ etc\r\n});\r\n});<\/pre>\n<\/div>\n<p>The event object in the above callback handlers are related to the\u00a0add Itemcustom event and have nothing to do with the earlier click event.<\/p>\n<p>Now the logic is nicely separated, and additional chunks of functionality can be added or removed without any need to worry about the other parts of the application.<\/p>\n<p>And seeing as the global\u00a0document\u00a0object was chosen to be the mediator, these events can be attached in other places (perhaps a different file completely) without any extra effort.<\/p>\n<p>It seems like a bit more work initially, but as the code expands, this way of decoupling events will start to pay back in droves.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Decoupling is when we remove these unnecessary relationships, allowing units of code to function independently. Applications should be able to function independently or with other apps in a container (think widgets). While the concept of\u00a0custom events in jQuery\u00a0may not be a new one, I tend to only see them used amongst plugins, mainly to [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":719,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3,6],"tags":[115,60],"class_list":["post-557","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery","category-technical","category-tutorials","tag-decoupling","tag-jquery-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Decoupling with custom jQuery events - Siri Innovations<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Decoupling with custom jQuery events - Siri Innovations\" \/>\n<meta property=\"og:description\" content=\"Introduction: Decoupling is when we remove these unnecessary relationships, allowing units of code to function independently. Applications should be able to function independently or with other apps in a container (think widgets). While the concept of\u00a0custom events in jQuery\u00a0may not be a new one, I tend to only see them used amongst plugins, mainly to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/\" \/>\n<meta property=\"og:site_name\" content=\"Siri Innovations\" \/>\n<meta property=\"article:published_time\" content=\"2014-07-09T11:56:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-10T09:33:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"540\" \/>\n\t<meta property=\"og:image:height\" content=\"241\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yuva kumar\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yuva kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/\"},\"author\":{\"name\":\"Yuva kumar\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/e2941c65eca1eecb5bf7e44fb9f6f9c0\"},\"headline\":\"Decoupling with custom jQuery events\",\"datePublished\":\"2014-07-09T11:56:06+00:00\",\"dateModified\":\"2019-04-10T09:33:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/\"},\"wordCount\":450,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg\",\"keywords\":[\"Decoupling\",\"jquery\"],\"articleSection\":[\"jQuery\",\"Technical\",\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/\",\"url\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/\",\"name\":\"Decoupling with custom jQuery events - Siri Innovations\",\"isPartOf\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg\",\"datePublished\":\"2014-07-09T11:56:06+00:00\",\"dateModified\":\"2019-04-10T09:33:08+00:00\",\"author\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/e2941c65eca1eecb5bf7e44fb9f6f9c0\"},\"breadcrumb\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#primaryimage\",\"url\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg\",\"contentUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg\",\"width\":540,\"height\":241,\"caption\":\"jquery\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/siriinnovations.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Decoupling with custom jQuery events\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#website\",\"url\":\"https:\/\/siriinnovations.com\/blog\/\",\"name\":\"Technical blog from Siri Innovations\",\"description\":\"Innovative like no other\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/siriinnovations.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/e2941c65eca1eecb5bf7e44fb9f6f9c0\",\"name\":\"Yuva kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/687772b38575bc5be6e95c834b31796b82c9a71158f171c0323ac991815f38b2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/687772b38575bc5be6e95c834b31796b82c9a71158f171c0323ac991815f38b2?s=96&d=mm&r=g\",\"caption\":\"Yuva kumar\"},\"url\":\"https:\/\/siriinnovations.com\/blog\/author\/yuvakumar\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Decoupling with custom jQuery events - Siri Innovations","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/","og_locale":"en_US","og_type":"article","og_title":"Decoupling with custom jQuery events - Siri Innovations","og_description":"Introduction: Decoupling is when we remove these unnecessary relationships, allowing units of code to function independently. Applications should be able to function independently or with other apps in a container (think widgets). While the concept of\u00a0custom events in jQuery\u00a0may not be a new one, I tend to only see them used amongst plugins, mainly to [&hellip;]","og_url":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/","og_site_name":"Siri Innovations","article_published_time":"2014-07-09T11:56:06+00:00","article_modified_time":"2019-04-10T09:33:08+00:00","og_image":[{"width":540,"height":241,"url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg","type":"image\/jpeg"}],"author":"Yuva kumar","twitter_misc":{"Written by":"Yuva kumar","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#article","isPartOf":{"@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/"},"author":{"name":"Yuva kumar","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/e2941c65eca1eecb5bf7e44fb9f6f9c0"},"headline":"Decoupling with custom jQuery events","datePublished":"2014-07-09T11:56:06+00:00","dateModified":"2019-04-10T09:33:08+00:00","mainEntityOfPage":{"@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/"},"wordCount":450,"commentCount":0,"image":{"@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#primaryimage"},"thumbnailUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg","keywords":["Decoupling","jquery"],"articleSection":["jQuery","Technical","Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/","url":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/","name":"Decoupling with custom jQuery events - Siri Innovations","isPartOf":{"@id":"https:\/\/siriinnovations.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#primaryimage"},"image":{"@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#primaryimage"},"thumbnailUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg","datePublished":"2014-07-09T11:56:06+00:00","dateModified":"2019-04-10T09:33:08+00:00","author":{"@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/e2941c65eca1eecb5bf7e44fb9f6f9c0"},"breadcrumb":{"@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#primaryimage","url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg","contentUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg","width":540,"height":241,"caption":"jquery"},{"@type":"BreadcrumbList","@id":"https:\/\/siriinnovations.com\/blog\/decoupling-custom-jquery-events\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/siriinnovations.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Decoupling with custom jQuery events"}]},{"@type":"WebSite","@id":"https:\/\/siriinnovations.com\/blog\/#website","url":"https:\/\/siriinnovations.com\/blog\/","name":"Technical blog from Siri Innovations","description":"Innovative like no other","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/siriinnovations.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/e2941c65eca1eecb5bf7e44fb9f6f9c0","name":"Yuva kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/687772b38575bc5be6e95c834b31796b82c9a71158f171c0323ac991815f38b2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/687772b38575bc5be6e95c834b31796b82c9a71158f171c0323ac991815f38b2?s=96&d=mm&r=g","caption":"Yuva kumar"},"url":"https:\/\/siriinnovations.com\/blog\/author\/yuvakumar\/"}]}},"jetpack_featured_media_url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2013\/09\/jquery.jpg","_links":{"self":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/comments?post=557"}],"version-history":[{"count":11,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/557\/revisions"}],"predecessor-version":[{"id":1011,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/557\/revisions\/1011"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/media\/719"}],"wp:attachment":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/media?parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/categories?post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/tags?post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}