1

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.

Community
  • 1
  • 1
Jakub Žitný
  • 962
  • 1
  • 9
  • 38
  • The linked question should answer your question. If not, then you should specify in your question what it is specifically that it does not address. FYI, RequireJS does not have a way to tell you ahead of time whether a module exists or not. You load it and then process the error. Also, there is no such thing as an "unnamed module". In your code the variable `viewName` contains the name of the module you are loading. I'm guessing you meant to say something like "a module whose name is not known in advance". – Louis Apr 02 '14 at 12:27
  • By unnamed module I meant a module which is loaded by the name of the file and not by its name defined in `define("name", [deps], fc(){//...});`. Thanks for the info about not knowing the module existence, however, the [question I referenced](http://bit.ly/1opSr4f) seems to contradict that, or not? Also thanks for the linked question. JS console actually shows two errors linked to this. I tried to pass the error callback to require() and it did catch one of them (requirejs' Error: Script error for non-existing file), but it did not prevent the 404 Error. Any idea how to catch that one? – Jakub Žitný Apr 02 '14 at 13:27
  • 1
    Regarding unnamed module, a term like "a `define` without a name" would be better than "unnamed module" because as you know the module gets a name from the file name rather than the `define` call in that case. Regarding the 404 error in the console, there is no way to prevent the console from showing it. This is how RequireJS knows the module does not exist: it tries to load it, gets a 404 (which the browser, quite independently from RequireJS, shows in the console) and then the errback is called. In other words, even in code that works *perfectly*, you'll see 404 in the console. – Louis Apr 02 '14 at 13:31

0 Answers0