I want to require unnamed module in another requirejs' module, but only if it exists. Someone already seeked help with similar problem in this question, but it doesn't seem to work with unnamed modules. Is there any way to check if the module exists before loading it or just load it anyway and catch the js errors if it doesn't exists, please?
The scenario I need this for is following: I am transforming an big old project into require.js and I want to move all the javascript from templates (of an unnamed PHP framework) to separate modules in directory js/views. Before loading a specific page, another requirejs module will load corresponding js/views/. The loading module looks like this so far:
require(["jquery", "domReady!", "require"], function($, domReady, require) {
var domBody = document.querySelector('body');
var viewName = "views/" + domBody.dataset.view;
var view;
view = require([viewName]);
});
It loads the desired view-module name from body's data-view attribute and then based on that it loads the js/views/viewName module where all the javascript for that page is. The view-module is unnamed and loaded just by its filename and it works well for most of the cases. But I want to prevent the errors when the view-module file doesn't exist (some pages don't have any javascripts in them).
I know this is a bit ugly, but there is a lot of javascript inside the pages and this is the quickest way to transition all that into something nicer and faster.
Thanks.