Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone.history.start() throws a TypeError when r.js -o is used
    text
    copied!<p>When I run the r.js optimizer over my Backbone.js app, I get this error:</p> <pre><code>Uncaught TypeError: undefined is not a function </code></pre> <p>It appears in the middle of the Backbone.js section of the output.</p> <p>I removed bits of code until I found out what was causing it, and it seems to be when I call <code>Backbone.history.start();</code>.</p> <p>I've created a simple app to replicate the bug, which I've posted below. Basically, it creates a route and 'console.log's 'home'. It works fine when not optimized.</p> <p>To optimize the app, I use:</p> <pre><code>node r.js -o app.build.js </code></pre> <p>from the /js folder.</p> <p>I have Backbone.js 0.9.2, RequireJS 2.1.1, r.js 2.1.1</p> <p>Has anyone come across this before? I'm still fairly new to Backbone.js/RequireJS, so hopefully it's just something stupid I'm doing wrong.</p> <h2>Folder Structure</h2> <pre><code>/js /libs - backbone.js - require.js - underscore.js - app.build.js - r.js - site.js index.html </code></pre> <h2>Code</h2> <h3>index.html</h3> <pre><code>&lt;!doctype html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;script data-main="/js/site.js" src="/js/libs/require.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h3>site.js</h3> <pre><code>requirejs.config({ baseUrl : 'js/', paths : { jquery : '//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min', backbone : 'libs/backbone', underscore : 'libs/underscore' }, shim : { backbone : { deps : ['underscore', 'jquery'], exports : 'Backbone' }, underscore : { exports : '_' } } }); require(['jquery', 'backbone'], function($, Backbone) { var Router = Backbone.Router.extend({ routes : { '' : 'home' }, home : function(){ console.log('home'); } }), appRouter; $(function() { appRouter = new Router(); Backbone.history.start(); }); }); </code></pre> <h3>app.build.js</h3> <pre><code>({ baseUrl: ".", name: 'site', paths: { jquery: 'empty:', underscore: 'libs/underscore', backbone: 'libs/backbone' }, out: 'site-built.js', shim : { backbone : { deps : ['underscore', 'jquery'], exports : 'Backbone' }, underscore : { exports : '_' } } }) </code></pre> <p>Thanks!</p> <h2>Update</h2> <p>Here's the output of site-built.js. Can anyone see anything obviously wrong with it?</p> <p><a href="http://pastebin.com/Rbm7J83X" rel="nofollow">http://pastebin.com/Rbm7J83X</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload