Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Require conditional loading
define(['module', 'jquery'], function(module, $) {
return {
//in a plugin, we always give a load function for require to execute
load: function(id, require, load, config) {
//set up an object matching up a "type" to a library path. The paths I included may not be correct, I just roughed them out
var typeToLoad = {
'video': 'ap/video',
'audio': 'ap/audio'
//the "type" should be attached to the overall module back on app.js, here I assume it's saved to the module as questionType
var rawQuestionType = module.questionType
//Use the "path" for this question, and our lookup object, to determine what conditional path to load
//If the "type" is not found in our lookup object, we return false
var questionLibPath = typeToLoad[rawQuestionType] || false;
//if this question's "type" has a conditional path, then load that conditional path
if (questionLibPath) {
require([questionLibPath], load);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment