Mjt.freebase is a browser Javascript library for working with Freebase.
This is a full-featured Freebase client library, with good coverage of the Freebase APIs and many helpful utility functions.
Mjt.freebase provides:
Mjt.freebase has been used to build the Freebaseapps Application Editor, a full-featured in-browser development environment for server-side Javascript applications.
Mjt.freebase can be used independently of the mjt Templating system or mjt.App framework.
Mjt.freebase is layered on top of the mjt.Task asynchronous task framework, which provides timeouts and makes it easy to describe complex dependencies. Some basic information is given here. For more information, see the mjt.Task documentation.
A newly created task starts life in init state:
var task = mjt.freebase.MqlRead({...});
Once prepared, the task must be "enqueued" putting it in wait state:
task.enqueue();
When the task completes it will move to ready or error state. Separate callbacks can be attached to handle success and failure:
task.onready(function (result) { ... });
task.onerror(function (code, msg, info) { ... });
Most task operations can be chained for a more compact syntax:
var task = mjt.freebase.MqlRead({...})
.enqueue()
.onready(function (result) { ... })
.onerror(function (code, msg, info) { ... });
The Freebase data store contains user-contributed content, and developers should be careful not to execute any Javascript of unknown authorship. Mjt.freebase was designed for use with the mjt.Template template system which offers good protection against cross-site scripting (XSS) by quoting output when necessary. If you are formatting your own output from freebase data, you should be careful to call mjt.htmlencode() as needed.
Read access to Freebase is done using dynamic <script> tags and JSONP whenever possible. This is a very fast form of I/O and allows queries from any webpage directly to Freebase.
Queries too large to fit in a single URL may require XMLHttpRequest support, and mjt.freebase will attempt to fall back on that method.
Wraps api/service/mqlread.
The result is a Javascript object that contains a property called "result" if the query was successful.
Wraps api/service/mqlread, but allows multiple MqlRead tasks to be combined into a single HTTP request. Subtasks must be added before calling .enqueue():
var read1 = mjt.freebase.MqlRead({... query 1 ... });
var read2 = mjt.freebase.MqlRead({... query 2 ... });
var reads = mjt.freebase.MqlReadMultiple()
.mqlread(read1);
.mqlread(read2);
.enqueue();
When the call completes, each of the subtasks will run its onready or onerror handlers.
Wraps api/service/touch.
Updates the browser MWLastWriteTime cookie which is necessary for getting the latest content from other Freebase APIs.
Using this call will cause your Freebase queries to return fresher results, but your queries may run slower. Use sparingly.
Wraps api/trans/raw.
Retrieves content from the content store given a document's ID.
You can invoke api/trans/blurb using TransGet(doc_id, 'blurb');
Privileged services are only accessible in the browser using XMLHttpRequest, which imposes a same origin restriction. Only web pages hosted at freebase.com are able to write directly to the HTTP services hosted there. Other applications must provide a local HTTP proxy to the service entry points at http://api.freebase.com/api and configure mjt.freebase to point to that proxy.
Mjt.freebase uses the variable mjt.freebase.service_url to generate API URLs.
Wraps api/trans/unsafe.
The TransGet() API sanitizes HTML and other formats that may contain executable code. If you absolutely need access to the raw data from a document, use TransUnsafe().
Wraps api/service/mqlwrite.
Performs a MQL write.
Uploads content to the content store.
Converts an ISO8601 date string returned in MQL to Javascript Date object. Freebase uses a subset of ISO8601 dates, which are not handled natively by Javascript.
Converts a Javascript Date object to an ISO8601 date string usable in MQL.
Returns a URL that can be used as the src attribute of an <img> tag. The generated url refers to api/trans/image_thumb.
Overlay new key/value pairs in the given obj into an existing query.