BoilerplateMVC / Marionette-Require-Boilerplate

Marionette-Require-Boilerplate
MIT License
242 stars 61 forks source link

CollectionView or CompositeView give console error in "production" mode #8

Closed qaiser110 closed 11 years ago

qaiser110 commented 11 years ago

My MRB app runs fine in dev-mode, but as soon as I "grunt" and turn on "production" mode, I get the following error:

Uncaught TypeError: Cannot read property 'CollectionView' of undefined

I changed the CollectionView to CompositeView, but then I get:

Uncaught TypeError: Cannot read property 'CompositeView' of undefined

qaiser110 commented 11 years ago

Just to clarify, I'm developing on Windows 7, without Node.js.

Also, my App works fine in "Dev" mode but breaks (on both Chrome and Firefox) only in "Production Mode", i.e. with minified JS file.

I'm not an expert, but I think Issue #8 is related to r.js Optimizer messing up the dependencies during minification. I think Marionette.CollectionView is being called before Marionette is available, although I'm loading both Backbone and Marionette in the define() call.

brettjonesdev commented 11 years ago

Oh, wait - I think I might know what this is. Are you referencing Marionette directly in your code? i.e.

define(['backbone', 'marionette'], function(Backbone, Marionette) {
    return Marionette.ItemView.extend({/*whatever*/});
});

This will actually fail in your production build. The proper way to do this is to only ever reference Marionette via Backbone.Marionette, like so:

define(['backbone', 'marionette'], function(Backbone) {
    return Backbone.Marionette.ItemView.extend({/*whatever*/});
});

I ran into this issue myself before. I am not exactly sure why the shim configuration that specifies Marionette exports the Marionette object fails to work once built. Perhaps it may be best to remove the shim configuration in order to make this fail in dev as well.

Let me know if this fixes your issue.

qaiser110 commented 11 years ago

Thanks Brett. It did fix my issue. I had return Marionette.CollectionView in one of my modules, which was causing it.

I'm also getting this error in "Production" mode in Chrome:

Uncaught Error: Mismatched anonymous define() module: [object Object]

Although this is not causing my App to break, and it's related to RequireJS, I was wondering if you know the fix for this issue.

brettjonesdev commented 11 years ago

Read this and see if you are doing any of those things. My guess would be that you have defined an anonymous module within your HTML, as opposed to in a .js file. Something like this:

<html>
<head>
    <script type="text/javascript">
       //anonymous module
       define([], function(){});
    </script>
</head>

Note that an anonymous module is the right way to write modules in standalone .js files, but is not correct if you are defining a module within your html page. This is because anonymous modules are defined by the path/filename in which they are defined.

Hopefully that link will help.

qaiser110 commented 11 years ago

There's no anonymous module. In fact, when I install MRB without a single modification, this error is displayed in Chrome.

brettjonesdev commented 11 years ago

Oh wow... haha yikes. I will look into this when I have time. Thanks for pointing this out.

brettjonesdev commented 11 years ago

Are you still experiencing this? I just cloned a fresh copy of the project, turned production mode on, re-ran grunt and everything worked just fine. I am working on OS X but I wrote this originally on a Win7 machine. I am not sure what the problem would be. I would need a bit more info to be able to pursue this further. I am going to close this for now.

qaiser110 commented 11 years ago

You are right. The error has disappeared, even in my old built, even though I haven’t made any changes in that project. This has happened to me before in chrome, when an error appeared for no reason, and disappeared in the next chrome built. However, unlike the previous error, this one was also showing in Canary. Anyways, thanks for your time and a great boilerplate.

brettjonesdev commented 11 years ago

Phew I was worried there for a second! Yeah, sometimes Chrome goes crazy with its caching and you have to like reformat your harddrive before the cached version of a file will refresh...