0

I have an AngularJS application with RequireJS and JSHint. There I have a controller MyCtrl:

define([
    'myDependecy'
], function(app) {
    'use strict';
    app.controller('MyCtrl', function($scope) {
        doSomething();
        var test = MyTest.doIt();
    });
});

myDependency is a JavaScript file which has following content:

var MyTest = (function() {
    var doIt = function() {
        // …
    };
    return {
        doIt: doIt
    }
});

function doSomething() {
    console.log("test");
}

The problem with JSHint is that I get the error messages 'MyTest' is not defined and also 'doSomething' is not defined. What can I do to solve this problem, because they are defined but JSHint don't know it.

Update

The problem seems to be the mix of AMD and Non-AMD modules. So the best solution in this case won't be the /* global … */ comment.

Update2

The problem occurs also in grunt serve:dist which generates one big JavaScript file. So the problem is more "complex". The real problem has one more dependency. The MyTest object literal is here:

(function(root) {
    define("test1", ["test2"], function() {
        return (function() {
            var MyTest = (function () {
                // …
            }());
        }).apply(root, arguments);
    });
}(this));

And the implementation with the doIt is defined in a similar define block named test2 where the error occurs.

Tim
  • 13,228
  • 36
  • 108
  • 159
  • You can add `/*global MyTest*/` to tell JSHint about the variable. – Felix Kling Aug 20 '14 at 13:22
  • It works. So this is the solution or is it possible to do it in a better way? – Tim Aug 20 '14 at 13:27
  • 1
    This is not how you're supposed to use RequireJS; you're mixing AMD and non-AMD modules. If MyDependency followed AMD format the JSHint warnings would simply go away. Relevant documentation: http://requirejs.org/docs/api.html#define – kryger Aug 20 '14 at 14:14
  • I updated the original post, because doing a ``grunt serve:dist`` it will be wrapped. Also I thought in my ``main.js`` I have in ``require.congig`` a ``shim``section with ``test1: { deps: ['test2']}``. I think building with ``grunt serve:dist`` it is wrapped based on this configurations. – Tim Aug 21 '14 at 08:29

0 Answers0