Labs/Ubiquity/Ubiquity 0.5 Author Tutorial: Difference between revisions

Made the sample code modern (and actually work).
(Made the sample code modern (and actually work).)
Line 860: Line 860:
<pre>
<pre>
var noun_type_freebase_topic = {
var noun_type_freebase_topic = {
   _name: "Freebase topic",
   name: "Freebase topic",
    
   label: "topic",
   suggest: function suggest( text, html, callback ) {
   suggest: function freebase_topic_suggest(text, html, callback) {
     jQuery.ajax( {
     return [
      url: "http://www.freebase.com/api/service/search",
      CmdUtils.makeSugg(text),
      dataType: "json",
      $.ajax({
      data: { prefix: text, limit: 5 },
        url: "http://www.freebase.com/api/service/search",
      success: function suggestTopics( response ) {
        dataType: "json",
        var i, results, result;
        data: {prefix: text, limit: 5},
        results = response.result;
        success: function suggestTopics(response) {
        for ( i = 0; i < results.length; i++ ) {
          if (response.status === "200 OK")
          result = results[ i ];
            callback([CmdUtils.makeSugg(result.name, result.name, result)
          callback( CmdUtils.makeSugg( result.name, result.name, result ) );
                      for each (result in response.result)]);
         }
         },
       }
       })];
    } );
    return [CmdUtils.makeSugg(text)];
   }
   }
}
};
 
 
CmdUtils.CreateCommand( {
CmdUtils.CreateCommand({
   names: ["lookup on freebase"],
   names: ["lookup on freebase"],
   arguments: [{ role: "object",
   arguments: {object: noun_type_freebase_topic},
                nountype: noun_type_freebase_topic,
   preview: function preview(pblock, args) {
                label: "topic" },
     pblock.innerHTML = _(("Go to the Freebase topic page for " +
   preview: function preview( pblock, arguments ) {
                          "{if text}${text}{else}any topic{/if}."),
    var text = arguments.object.text || _("any topic");
                         args.object);
     pblock.innerHTML = _("Go to the Freebase topic page for ${text}.",
                         {text: text});
   },
   },
   execute: function goToFreebase( arguments ) {
   execute: function goToFreebase({object: {text, data}}) {
    if ( arguments.object.text ) {
    Utils.openUrlInBrowser(
      Utils.openUrlInBrowser( "http://www.freebase.com/view" + arguments.object.data.id );
      text
    }
      ? (data
        ? "http://www.freebase.com/view" + data.id
        : ("http://www.freebase.com/search?query=" +
            encodeURIComponent(text)))
      : "http://www.freebase.com/");
   }
   }
} );
});
</pre>
</pre>


73

edits