{"id":1492,"date":"2014-03-21T21:01:20","date_gmt":"2014-03-22T02:01:20","guid":{"rendered":"http:\/\/unitstep.net\/?p=1492"},"modified":"2014-03-21T21:07:40","modified_gmt":"2014-03-22T02:07:40","slug":"requirejs-shim-configuration-jquery-plugins-and-enforcedefine","status":"publish","type":"post","link":"https:\/\/unitstep.net\/blog\/2014\/03\/21\/requirejs-shim-configuration-jquery-plugins-and-enforcedefine\/","title":{"rendered":"RequireJS shim configuration, jQuery plugins and enforceDefine"},"content":{"rendered":"

If you’re using RequireJS’s<\/a> shim configuration<\/a> to load jQuery plugins while setting enforceDefine: true<\/code> in your configuration, you probably noticed the following uncaught error in your JavaScript console: (The following example is for the iCheck <\/a>plugin)<\/p>\n

Uncaught Error: No define call for iCheck\r\nhttp:\/\/requirejs.org\/docs\/errors.html#nodefine<\/a><\/code><\/pre>\n

Reading the documentation at that URL<\/acronym>, yields the following important tidbit. (Emphasis mine)<\/p>\n

\nThis occurs when enforceDefine is set to true, and a script that is loaded either:<\/p>\n

Did not call define() to declare a module.
\nOr was part of a shim config that specified a string exports property that can be checked to verify loading, and that check failed.
\nOr was part of a shim config that did not set a string value for the exports config option.<\/strong>\n<\/p><\/blockquote>\n

This means if you have probably defined your shim config like so, because you technically don’t need any exports from a jQuery plugin: (Since they just extend the jQuery object)<\/p>\n

 shim: {\r\n    \"iCheck\": [\"jquery\"],\r\n  },<\/code><\/pre>\n

This doesn’t define an exports<\/code>, so that is the cause of the error. To fix this, you can simply define the export to be the function the plugin adds to the jQuery object:<\/p>\n

  shim: {\r\n    \"iCheck\": {\r\n      deps: [\"jquery\"],\r\n      exports: \"jQuery.fn.iCheck\",\r\n    }\r\n  },<\/code><\/pre>\n

You can then reference the shim configured module (in this case, iCheck<\/code>) using a requirejs()<\/code> or define()<\/code> call.<\/p>","protected":false},"excerpt":{"rendered":"

If you’re using RequireJS’s shim configuration to load jQuery plugins while setting enforceDefine: true in your configuration, you probably noticed the following uncaught error in your JavaScript console: (The following example is for the iCheck plugin) Uncaught Error: No define call for iCheck http:\/\/requirejs.org\/docs\/errors.html#nodefine Reading the documentation at that URL, yields the following important tidbit. […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,137],"tags":[],"_links":{"self":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/1492"}],"collection":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/comments?post=1492"}],"version-history":[{"count":12,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/1492\/revisions"}],"predecessor-version":[{"id":1508,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/1492\/revisions\/1508"}],"wp:attachment":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/media?parent=1492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/categories?post=1492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/tags?post=1492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}