1

I'm new to require.js so I may not be using it correctly, but as far as I know, I can't find what's causing the error.

Structure

index.html
Scripts\
  - jquery-2.1.1.min.js
  - require.js
  \app
    - main.js
  \lib
    - work-requests.js
    - controller-base.js
    - util.js

index.html

<script src="Scripts/require.js" data-main="Scripts/app/main"></script>

main.js

requirejs.config({
    baseUrl: 'Scripts/lib',
    paths: {
        jquery: '../jquery-2.1.1.min',
    },
});
requirejs(['jquery', 'util', 'work-requests'], function ($, util, work_requests) {
    $(function () {
    var foo = new work_requests.get_all(); // <-- error "Uncaught TypeError: undefined is not a function
});

work-requests.js

define(['controller-base'], function (controller_base) {
    return function () {
        var that = controller_base('work-requests')

        return that;
    }
});

controller-base.js

define(['jquery'], function ($) {
    return function (controller_name) {
        var that = {};
        that.controller = controller_name;

        that.get_all = function () {
            return $.ajax({
                type: 'get',
                contentType: 'application/json',
                url: _api_root + '/' + that.controller,
            });
        };
        return that;
    };
});

util.js

define({
  foo: function(data){},
});

2 Answers 2

2

Your require.js setup is completely fine. Did you try changing

var foo = new work_requests.get_all();

to

var foo = new work_requests();
var bar = foo.get_all();

?

Sign up to request clarification or add additional context in comments.

Comments

-1

Duplicate.

You're looking for mainConfigFile: 'path/to/main.js' requireJS optimization: undefined is not a function

1 Comment

Apparently this is a dupe. Where is the other question? I can't find it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.