Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
custom:javascript:lib:modopts.js [2019/01/07 23:11] – created digital man | custom:javascript:lib:modopts.js [2019/07/17 00:25] (current) – Use syntax highlighting digital man | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== modopts.js ====== | ====== modopts.js ====== | ||
- | FIXME | + | |
+ | If you are a Synchronet JavaScript module author or modder and you want to easily add some sysop-configurable options to your module, this is where the Module Options ('' | ||
+ | |||
+ | ===== Usage ===== | ||
+ | |||
+ | ==== modopts.ini ==== | ||
+ | |||
+ | - Add a new section to your '' | ||
+ | - Choose some key names for the options you need in your script. Use legal JavaScript variable names. | ||
+ | |||
+ | For example: | ||
+ | <file ini> | ||
+ | [yourmodule] | ||
+ | good_thing = true | ||
+ | bad_thing = false | ||
+ | other_thing = some text | ||
+ | </ | ||
+ | |||
+ | ==== yourmodule.js ==== | ||
+ | |||
+ | You need to execute (via '' | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <code javascript> | ||
+ | var options = load({}, " | ||
+ | </ | ||
+ | |||
+ | Now if there is any problem reading the '' | ||
+ | |||
+ | <code javascript> | ||
+ | var options = load({}, " | ||
+ | if(!options) | ||
+ | options = {}; | ||
+ | </ | ||
+ | |||
+ | Now you can safely reference '' | ||
+ | |||
+ | <code javascript> | ||
+ | if(options.good_thing == true) | ||
+ | | ||
+ | if(options.bad_thing == true) | ||
+ | | ||
+ | print(options.other_thing); | ||
+ | </ | ||
+ | |||
+ | === Default option values === | ||
+ | |||
+ | If you want to assign default option values, you can assign them right after you '' | ||
+ | |||
+ | <code javascript> | ||
+ | var options = load({}, " | ||
+ | if(!options) | ||
+ | options = {}; | ||
+ | if(options.good_thing === undefined) | ||
+ | | ||
+ | if(options.bad_thing === undefined) | ||
+ | | ||
+ | if(options.other_thing === undefined) | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Or with string values, you can easily specify defaults //in-line// using the '' | ||
+ | |||
+ | <code javascript> | ||
+ | print(options.other_thing || "some text" | ||
+ | </ | ||
+ | |||
+ | === Options Types === | ||
+ | |||
+ | When using '' | ||
+ | |||
+ | * //Number//: any value that is a valid decimal or floating point number or a valid hexadecimal number beginning with '' | ||
+ | * // | ||
+ | * //String//: every other value | ||
+ | |||
+ | If you would like to use a different or more precise value/ | ||
+ | |||
+ | For example, to load a //Date// option value: | ||
+ | |||
+ | <code javascript> | ||
+ | var date_option = load({}, " | ||
+ | </ | ||
+ | |||
+ | For example, to load a //String// option (even when it's a valid //Number// or //Boolean// value): | ||
+ | |||
+ | <code javascript> | ||
+ | var str_option = load({}, " | ||
+ | </ | ||
+ | |||
+ | When using '' | ||
+ | |||
+ | * Number | ||
+ | * Boolean | ||
+ | * String | ||
+ | * Date | ||
+ | * Array (comma-separated list of string values) | ||
+ | |||
===== See Also ===== | ===== See Also ===== | ||
* [[: | * [[: | ||
+ | * [[: | ||
+ | * [[: | ||
{{tag> | {{tag> | ||