0

I have a JavaScript file named navigationView as follows:

define(['underscore', 'backbone'], function (_, Backbone) {
function navigationBuild() {
    $.ajax({
        async: false,
        url: "http://myapi/articles/categories",
        type: "GET",
        headers: { "Accept": "application/json;odata=verbose" },
        success: function (data) {

            // Append values to Navigation
            $.each(data, function (navigationindex, navigationvalue) {
                $('nav').append('<a href="#/' + navigationvalue.shortName + '">' + navigationvalue.displayName + '</a> ');
            });
        }
    });
}
});

I am trying to call this with Require.js so that it is loaded with each page. This builds my navigation. Here is my app.js where I attempt to have a function call, but it gives me an error of "Uncaught TypeError: Cannot read property 'navigationBuild' of undefined". I suspect that I need a return value in my navigationView file.

require.config({
paths: {
    jquery: "libs/jquery-2.1.0.min",
    underscore: "libs/underscore-min",
    backbone: "libs/backbone-min",
    navigation: "views/navigationView"
},
shim: {
    underscore: {
        exports: "_"
    },
    backbone: {
        deps: ['underscore', 'jquery'],
        exports: 'Backbone'
    },
}
});

require(['navigation'], function(navigation) {
navigation.navigationBuild();
});

require(['routers/siteRouter'], function (router) {
new router;
 });

How do I call functions from JavaScript files using Require.js?

1 Answer 1

1

In your navigation.js file you aren't returning anything. Modify it so that you assign your function to a variable and return that.

For example

define(['underscore', 'backbone'], function (_, Backbone) {
   var navigationBuild = function () {
    $.ajax({
        async: false,
        url: "http://myapi/articles/categories",
        type: "GET",
        headers: { "Accept": "application/json;odata=verbose" },
        success: function (data) {

            // Append values to Navigation
            $.each(data, function (navigationindex, navigationvalue) {
                $('nav').append('<a href="#/' + navigationvalue.shortName + '">' + navigationvalue.displayName + '</a> ');
            });
        }
    });
}

return navigationBuild;
});




require(['navigation'], function(navigation) {
   navigation();
});

That said it might be neater and more flexible to instead return an object that has methods, for example

define(['underscore', 'backbone'], function (_, Backbone) {
  var navObj = {
     build: function () {
       ....
      }
   };


  return navObj;

});

require(['navigation'], function(navigation) {
   navigation.build();
})
Sign up to request clarification or add additional context in comments.

Comments

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.