166
edits
Line 364: | Line 364: | ||
jQuery ist ein wirklich mächtiges Werkzeug. Damit kannst Du ziemlich problemlos Deine gewünschten Daten aus einem RSS-Feed herauspicken und Vorschau-Animationen sind damit ein Kinderspiel. | jQuery ist ein wirklich mächtiges Werkzeug. Damit kannst Du ziemlich problemlos Deine gewünschten Daten aus einem RSS-Feed herauspicken und Vorschau-Animationen sind damit ein Kinderspiel. | ||
== Color: Creating Bounded Noun Types == | |||
Suppose you're writing a set of commands for artists and | |||
web designers, and you know that several of the commands will operate | |||
on colors. You'd like to be able to specify that certain commands | |||
expect names of colors as arguments. Since there are a finite number of | |||
named colors, you can define a custom noun type for them based on a list | |||
of strings, like so: | |||
<pre> | |||
noun_type_color = new CmdUtils.NounType( "Color", | |||
["red", "orange", "yellow", "green", "blue", "violet", "black", "white", | |||
"grey", "brown", "beige", "magenta", "cerulean", "puce"] // etc... | |||
); | |||
</pre> | |||
Note that we gave the new object a name starting with "<code>noun_</code>". | |||
The Ubiquity command loader automatically detects objects starting with | |||
"<code>noun_</code>" as custom noun-types, in the same way as it auto-detects | |||
functions starting with "<code>cmd_</code>" as custom commands. | |||
Once you've defined a custom noun-type, you can use it in as many commands | |||
as you like, thus: | |||
<pre> | |||
CmdUtils.CreateCommand({ | |||
name: "get-color-code", | |||
takes: {"color": noun_type_color}, | |||
preview: "Inserts the HTML hex-code for the given color.", | |||
// etc... | |||
</pre> | |||
One benefit of creating the custom color noun-type is that if the user | |||
has entered "get-color bl", for instance, Ubiquity will be able to suggest | |||
"black" and "blue" as the two valid completions based on the input. | |||
Of course, not every type of noun you'd be interested in can be represented | |||
as a finite list. If you want to be able to accept or reject input based | |||
on some algorithmic test, you can do so by creating your own noun-type | |||
implementation instead of instantiating <code>CmdUtils.NounType</code>. There is an example of this in the section on the tab commands, below. |
edits