Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Excerpt

Some tricks with the include() function...

 

Including a file if some other file not loaded

You can use this trick to create mods for your mods. A basic approach can be done using just the include() function and nothing else...

Code Block
themeRDark
languagejavascript
titleBasic approach to loading defaults if alternate not found...
linenumberstrue
if (include("path/to/mymod.custom.js") === false) { // not found
  include("path/to/mymod.default.js"); // load default settings
}

But, assuming you're starting to use dependencies, you might want a more robust approach...

Code Block
themeRDark
languagejavascript
titleUse dependency checking to load file and check it initialised...
linenumberstrue
include("path/to/util.js");
 
Check.paths = ["path/to/mymod"]; // enable autoloading
 
if (!Check.has("mymod.custom.js")) { // path/to/mymod.custom.js
	Check.load("mymod.default.js");  // path/to/mymod.default.js
}

The obvious problem here is that you might have dependencies that rely on "mymod.default.js", but if "mymod.custom.js" gets loaded instead those dependencies will break.

The solution to this is to provide a fake file in both "mymod.default.js" and "mymod.extend.js" and then use that for dependency checks:

Code Block
themeRDark
languagejavascript
titleFake provide in mymod.default.js and mymod.extend.js
firstline52
linenumberstrue
// The usenormal thisprovide() iflets you wantcheck to checklater which settingsfile werewas loaded...
Check.provide(self);
 
// use this for general dependency checksThe fake provide() lets you check that any settings were loaded...
Check.provide({
	file: "settings.loaded",
	ver : self.ver
});

If you're particularly paranoid about having both file somehow get loaded, you can make sure "settings.loaded" isn't loaded during dependency checks at the top of those files...

Code Block
themeRDark
languagejavascript
titleMake sure settings not already loaded in mymod.default.js and mymod.extend.js...
firstline18
linenumberstrue
var dependencies = {
	"Util.js": Check.ANY_VERSION,
	"settings.loaded": Check.NOT_LOADED
}
 
// will throw error if "settings.loaded" already loaded...
Check.required(dependencies, self);
Div
classbox
Availability
Status
colourGreen
titleStable

Requires:

Div
classbox

Dependency Checking

Topics:

Child pages (Children Display)
alltrue
depthall
pageDependency Checking
excerpttrue