{"id":1744,"date":"2013-01-07T08:36:34","date_gmt":"2013-01-07T08:36:34","guid":{"rendered":"http:\/\/thekua.com\/atwork\/?p=1744"},"modified":"2019-07-18T16:13:12","modified_gmt":"2019-07-18T14:13:12","slug":"requirejs-is-the-spring-framework-of-javascript","status":"publish","type":"post","link":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/","title":{"rendered":"RequireJS is the Spring Framework of Javascript"},"content":{"rendered":"<p>I&#8217;ve been working on setting up the infrastructure for a mostly javascript based project, and we&#8217;ve been putting <a href=\"http:\/\/requirejs.org\/\" title=\"RequireJS\">RequireJS<\/a> into the codebase to help us manage the file dependencies instead of having to declare them within the page that is using them. As a concept, <a href=\"http:\/\/requirejs.org\/\" title=\"RequireJS\">RequireJS<\/a> is helping us keep different javascript modules apart in different files and let&#8217;s us assemble them. <\/p>\n<p><a href=\"http:\/\/requirejs.org\/\" title=\"RequireJS\">RequireJS<\/a> works by declaring dependencies and having the framework pull them in when you need them. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">define(&#x5B;&quot;aDependency&quot;], function(theDependency) {\r\n  \/\/ now I can do something with theDependency\r\n  theDependency.aMethodOnIt();\r\n})<\/pre>\n<p>This is pretty much how spring works, but the issue I have is that <a href=\"http:\/\/requirejs.org\/\" title=\"RequireJS\">RequireJS<\/a> manages the lifecycle of the javascript objects, so when you want to pass in a substitute for a test, you end up in a dilemma. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">define(&#x5B;&quot;aDependency&quot;], function(theDependency) { \/\/ how do I get inject a different instance?\r\n  \/\/ now I can do something with theDependency\r\n  theDependency.aMethodOnIt();\r\n})<\/pre>\n<p>Unsurprisingly a number of people wrote libraries such as <a href=\"https:\/\/github.com\/mattfysh\/testr.js\" title=\"testr\">testr<\/a> which allow you to override the requirejs to inject different versions. Although very reasonable approaches, I find this approach a little bit smelly as you&#8217;re effectively patching a library you don&#8217;t own. The ruby community know the dangers of monkey patching too much, particularly those parts of a code base you cannot control and the potential issues you face when you try to upgrade. <\/p>\n<p>Our current approach involves using <a href=\"http:\/\/requirejs.org\/\" title=\"RequireJS\">RequireJS<\/a> to manage the file\/name dependencies, but for us to write javascript that allows us to control the instances of the objects that we want. Here&#8217;s an example:<\/p>\n<p><strong>dependency.js<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">define(&#x5B;], function () {\r\n    return function () {\r\n        return {\r\n            doSomeWork:function () {\r\n            }\r\n        };\r\n    };\r\n});<\/pre>\n<p><strong>consumer.js<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">define(&#x5B;], function () {\r\n    return function (aDependency) {\r\n        var dependency = aDependency;\r\n        return {\r\n            start:function () {\r\n                dependency.doSomeWork();\r\n            }\r\n        };\r\n    };\r\n});<\/pre>\n<p>And then we control the lifecycle of the components and instances in the application using the following code. <\/p>\n<p><strong>main.js<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">define(&#x5B;&quot;consumer&quot;, &quot;dependency&quot;], function (Consumer, Dependency) {\r\n    var dependency = Dependency();\r\n    var consumer = Consumer(dependency);\r\n    consumer.start();\r\n});<\/pre>\n<p>And our jasmine tests get to look like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">requirejs = require('requirejs');\r\n\r\ndescribe(&quot;consumer&quot;, function() {\r\n    it(&quot;should ensure the dependency does some work&quot;, function() {\r\n        \/\/ given\r\n        var dependency = jasmine.createSpyObj(&quot;dependency&quot;, &#x5B;&quot;doSomeWork&quot;]);\r\n        var consumer = requirejs(&quot;consumer&quot;)(dependency);\r\n\r\n        \/\/ when\r\n        consumer.start();\r\n\r\n        \/\/ then\r\n        expect(dependency.doSomeWork).toHaveBeenCalled();\r\n    });\r\n});\r\n<\/pre>\n<p>This approach has been working out well, forcing us to manage the dependency and global hell that javascript global functions can quickly become. Thoughts? Please leave a comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on setting up the infrastructure for a mostly javascript based project, and we&#8217;ve been putting RequireJS into the codebase to help us manage the file dependencies instead of having to declare them within the page that is using them. As a concept, RequireJS is helping us keep different javascript modules apart in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,47],"tags":[],"class_list":["post-1744","post","type-post","status-publish","format-standard","hentry","category-development","category-javascript","post-preview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>RequireJS is the Spring Framework of Javascript - patkua@work<\/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:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"RequireJS is the Spring Framework of Javascript - patkua@work\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been working on setting up the infrastructure for a mostly javascript based project, and we&#8217;ve been putting RequireJS into the codebase to help us manage the file dependencies instead of having to declare them within the page that is using them. As a concept, RequireJS is helping us keep different javascript modules apart in [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"patkua@work\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-07T08:36:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-07-18T14:13:12+00:00\" \/>\n<meta name=\"author\" content=\"Patrick\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@patkua\" \/>\n<meta name=\"twitter:site\" content=\"@patkua\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Patrick\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/\"},\"author\":{\"name\":\"Patrick\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"headline\":\"RequireJS is the Spring Framework of Javascript\",\"datePublished\":\"2013-01-07T08:36:34+00:00\",\"dateModified\":\"2019-07-18T14:13:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/\"},\"wordCount\":446,\"commentCount\":8,\"articleSection\":[\"Development\",\"Javascript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/\",\"name\":\"RequireJS is the Spring Framework of Javascript - patkua@work\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\"},\"datePublished\":\"2013-01-07T08:36:34+00:00\",\"dateModified\":\"2019-07-18T14:13:12+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/requirejs-is-the-spring-framework-of-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"RequireJS is the Spring Framework of Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\",\"name\":\"patkua@work\",\"description\":\"The intersection of technology and leadership\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\",\"name\":\"Patrick\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"caption\":\"Patrick\"},\"sameAs\":[\"http:\\\/\\\/thekua.com\\\/atwork\"],\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/author\\\/patrick\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"RequireJS is the Spring Framework of Javascript - patkua@work","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:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/","og_locale":"en_US","og_type":"article","og_title":"RequireJS is the Spring Framework of Javascript - patkua@work","og_description":"I&#8217;ve been working on setting up the infrastructure for a mostly javascript based project, and we&#8217;ve been putting RequireJS into the codebase to help us manage the file dependencies instead of having to declare them within the page that is using them. As a concept, RequireJS is helping us keep different javascript modules apart in [&hellip;]","og_url":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/","og_site_name":"patkua@work","article_published_time":"2013-01-07T08:36:34+00:00","article_modified_time":"2019-07-18T14:13:12+00:00","author":"Patrick","twitter_card":"summary_large_image","twitter_creator":"@patkua","twitter_site":"@patkua","twitter_misc":{"Written by":"Patrick","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/#article","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/"},"author":{"name":"Patrick","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"headline":"RequireJS is the Spring Framework of Javascript","datePublished":"2013-01-07T08:36:34+00:00","dateModified":"2019-07-18T14:13:12+00:00","mainEntityOfPage":{"@id":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/"},"wordCount":446,"commentCount":8,"articleSection":["Development","Javascript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/","url":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/","name":"RequireJS is the Spring Framework of Javascript - patkua@work","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/#website"},"datePublished":"2013-01-07T08:36:34+00:00","dateModified":"2019-07-18T14:13:12+00:00","author":{"@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"breadcrumb":{"@id":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thekua.com\/atwork\/2013\/01\/requirejs-is-the-spring-framework-of-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thekua.com\/atwork\/"},{"@type":"ListItem","position":2,"name":"RequireJS is the Spring Framework of Javascript"}]},{"@type":"WebSite","@id":"https:\/\/thekua.com\/atwork\/#website","url":"https:\/\/thekua.com\/atwork\/","name":"patkua@work","description":"The intersection of technology and leadership","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thekua.com\/atwork\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae","name":"Patrick","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","caption":"Patrick"},"sameAs":["http:\/\/thekua.com\/atwork"],"url":"https:\/\/thekua.com\/atwork\/author\/patrick\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/comments?post=1744"}],"version-history":[{"count":8,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1744\/revisions"}],"predecessor-version":[{"id":3411,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1744\/revisions\/3411"}],"wp:attachment":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/media?parent=1744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/categories?post=1744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/tags?post=1744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}