Labs/Ubiquity/Ubiquity Source Tip Author Tutorial: Difference between revisions

Line 657: Line 657:
== Switching: Writing your own Noun-Types ==
== Switching: Writing your own Noun-Types ==


A noun-type needs to only have two things: A name and a suggest function. Soon, we'll probably move to having a convenience <code>CmdUtils.CreateNounType()</code>, which will simplify things even more.
A noun-type needs to only have two things: A <code>label</code> and a <code>suggest()</code> function. It can optionally also have a <code>default()</code> function.


The name is what shows up when the command prompts for input. Suggest returns a list of input objects, each one containing the name of a matching tab. We're using [http://developer.mozilla.org/en/docs/FUEL FUEL] to interact with the browser, which is where the "Application" variable comes from.
The label is what shows up when the command prompts for input. Suggest returns a list of input objects, each one containing the name of a matching tab. We're using [http://developer.mozilla.org/en/docs/FUEL FUEL] to interact with the browser, which is where the "Application" variable comes from.


<pre>
<pre>
var noun_type_tab = {
var noun_type_tab = {
   _name: "tab name",
   label: "tab name",


   // Returns all tabs from all windows.
   // Returns all tabs from all windows.
Line 680: Line 680:
   },
   },


   suggest: function( text, html ) {
   suggest: function( text, html, callback ) {
      
      
     var suggestions  = [];
     var suggestions  = [];
Line 699: Line 699:
The suggest method of a noun type always gets passed both text and html.  If the input is coming from a part of a web page that the user has selected, these
The suggest method of a noun type always gets passed both text and html.  If the input is coming from a part of a web page that the user has selected, these
values can be different: they are both strings, but the html value contains markup tags while the text value does not. The Tab noun type only cares about the plain text of the tab name, so we ignore the value of html.
values can be different: they are both strings, but the html value contains markup tags while the text value does not. The Tab noun type only cares about the plain text of the tab name, so we ignore the value of html.
The callback argument is for use by nountypes that need to run asynchronously, i.e. becaus they need to do network calls to generate suggestions.  The callback is a function; instead of returning a list of suggestions right away, an asynchronous noun type can call the callback with each suggestion it generates.  More on this later; the tab noun type generates all its suggestions right away, so it just returns them instead of using the callback.


We use the convenience function <code>CmdUtils.makeSugg()</code> to generate an
We use the convenience function <code>CmdUtils.makeSugg()</code> to generate an
1,007

edits