{"id":2902,"date":"2014-03-10T12:19:58","date_gmt":"2014-03-10T12:19:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/10\/loading-highcharts-via-shim-using-requirejs-and-maintaining-jquery-dependency-collection-of-common-programming-errors\/"},"modified":"2014-03-10T12:19:58","modified_gmt":"2014-03-10T12:19:58","slug":"loading-highcharts-via-shim-using-requirejs-and-maintaining-jquery-dependency-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/10\/loading-highcharts-via-shim-using-requirejs-and-maintaining-jquery-dependency-collection-of-common-programming-errors\/","title":{"rendered":"Loading Highcharts via shim using RequireJS and maintaining jQuery dependency-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m attempting to load the Highcharts library using a shim in RequireJS. However, when Highcharts loads, it throws an exception because it can&#8217;t access the jQuery methods it depends on.<\/p>\n<p>The require config looks like so:<\/p>\n<pre><code>require.config({\n    baseUrl: \"js\",\n\n    shim: {\n\n        'libs\/highcharts\/highcharts.src.js': {\n            deps: ['jquery'],\n            exports: function(jQuery)\n            {\n                this.HighchartsAdapter = jQuery;\n\n                return this.Highcharts;\n            }\n        }\n    }\n});\n<\/code><\/pre>\n<p>The exception that is thrown is:<\/p>\n<pre><code>Uncaught TypeError: undefined is not a function\n<\/code><\/pre>\n<p>and is in regards to this line:<\/p>\n<pre><code>dataLabels: merge(defaultLabelOptions, {\n<\/code><\/pre>\n<p>The issue is the <code>merge<\/code> call, which eventually maps itself back to jQuery (or some other adapter that Highcharts supports; but I&#8217;m just using jQuery).<\/p>\n<p>I&#8217;m not sure exactly how to make sure Highcharts gets access to jQuery using RequireJS and shim.<\/p>\n<p>Has anyone used RequireJS and Highcharts together before? I guess the issue isn&#8217;t specific to highcharts, but any library that has other sorts of dependencies.<\/p>\n<p>Thanks in advance for any advice or points to the correct direction!<\/p>\n<p><strong>To add further context, in hopes that someone who is familiar with require.js or shims will be able to help without having to be too intimately familiar with highcharts, here&#8217;s some source that sets up this <code>merge<\/code> method in Highcharts<\/strong><\/p>\n<pre><code>var globalAdapter = win.HighchartsAdapter,\nadapter = globalAdapter || {},\n\n\/\/ Utility functions. If the HighchartsAdapter is not defined, \n\/\/ adapter is an empty object\n\/\/ and all the utility functions will be null. In that case they are \n\/\/ populated by the\n\/\/ default adapters below.\n\n\/\/ {snipped code}\n\nmerge = adapter.merge\n\n\/\/ {snipped code}\n\nif (!globalAdapter &amp;&amp; win.jQuery) {\n    var jQ = win.jQuery;\n\n    \/\/ {snipped code}\n\n    merge = function () {\n        var args = arguments;\n        return jQ.extend(true, null, args[0], args[1], args[2], args[3]);\n    };\n\n    \/\/ {snipped code}\n}\n<\/code><\/pre>\n<p>The <code>win<\/code> object is a reference set up to <code>window<\/code> at the beginning of the script. So, I thought adding <code>window.jQuery = jQuery;<\/code> to the export method on the shim would result in highcharts picking up the jQuery reference; but it didn&#8217;t.<\/p>\n<p>Again, any insight, info, advice, or heckles would be appreciated at this point &#8211; I&#8217;m at a complete loss, and starting to question whether trying to implement and AMD package system in browser javascript is even worth it.<\/p>\n<p>After accepting the answer from <em>pabera<\/em> below I thought it appropriate to update my question to reflect how his answer helped my solution (though, it&#8217;s basically his answer).<\/p>\n<p>RequireJS uses &#8220;paths&#8221; to find libs that aren&#8217;t &#8220;AMD&#8221; supported and loads them on your page. the &#8220;shim&#8221; object allows you to define dependencies for the libraries defined in paths. The dependencies must be loaded before requirejs will try to load the dependent script.<\/p>\n<p>The exports property provides a mechanism to tell requirejs how to determine if the library is loaded. For core libs like jquery, backbone, socketio, etc they all export some window level variable (<code>Backbone<\/code>, <code>io<\/code>, <code>jQuery<\/code> and <code>$<\/code>, etc). You simply provide that variable name as the <code>exports<\/code> property, and requirejs will be able to determine when the lib is loaded.<\/p>\n<p>Once your definitions are done, you can use <code>requirejs<\/code>&#8216; <code>define<\/code> function as expected.<\/p>\n<p>Here&#8217;s my example <code>require.config<\/code> object:<\/p>\n<pre><code>require.config({\n    baseUrl: \"\/js\/\",\n\n    paths: {\n        jquery: 'jquery',\n        socketio: 'http:\/\/localhost:8000\/socket.io\/socket.io', \/\/for loading the socket.io client library\n        highcharts: 'libs\/highcharts\/highcharts.src',\n        underscore: 'libs\/underscore',\n        backbone: 'libs\/backbone'\n    },\n\n    shim: {\n        jquery: {\n            exports: 'jQuery'\n        },\n\n        socketio: {\n            exports: 'io'\n        },\n\n        underscore: {\n            exports: '_'\n        },\n\n        backbone: {\n            deps: ['jquery', 'underscore'],\n            exports: 'Backbone'\n        },\n\n        highcharts: {\n            deps: ['jquery'],\n            exports: 'Highcharts'\n        }\n    }\n});\n<\/code><\/pre>\n<p>As <em>pabera<\/em> mentioned before, this is for <code>Require.JS<\/code> version <code>2.0.1<\/code>.<\/p>\n<p>I hope someone gets some use out of this; I know it road blocked me for a little while; so hopefully we kept you from banging your head into the same spot in the wall that we did, by posting this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m attempting to load the Highcharts library using a shim in RequireJS. However, when Highcharts loads, it throws an exception because it can&#8217;t access the jQuery methods it depends on. The require config looks like so: require.config({ baseUrl: &#8220;js&#8221;, shim: { &#8216;libs\/highcharts\/highcharts.src.js&#8217;: { deps: [&#8216;jquery&#8217;], exports: function(jQuery) { this.HighchartsAdapter = jQuery; return this.Highcharts; } } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2902","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2902"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2902\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}