When you use the getObject. call of the Qlik Sense mashup API (also known as the Capabilities API or qlik.js) it works a bit like a black box: you just call it and it will inject some Qlik content into your page , you don’t have to bother about the content, it might be any type of chart, it might even be an extension. But it you want to, you can access the data of the visualization.
The key to this is the return value from getObject. The method returns, as it says in the documentation ‘a promise of an object model’. That means you can use the return value to get access to the data in the visualization, or even call some methods to modify the visualization automatically.
Only you have to understand:
- how a promise works
- and how to find data in the model you eventually get
The fact that getObject returns a Promise means that you will get an object with a then method. The then method will take a parameter, a function, that is called when getObject is ready, that is after a while, normally just some milliseconds, so to you it will look like immediately, but for the computer this is a long time.
What happens when you call getObject is:
- the browser will call the QIX engine over the web socket for the definition of the object with the id you supplied
- when the definition comes back it will call the engine to get the data
Only after these two calls will the promise resolve, and the method you supplied as a parameter to the then method will be called. That means that at this stage the object you get will be a model with valid data.
A good way to find out the contents of model is to use the debugger in your browser and inspect it. Some important parts of the model:
- layout: this is the data for the visualization. Expressions, hypercubes etc will be evaluated and contain data reflecting the current selections
- events, most important is the Validated event, which will be triggered when there is new valid data in the model. a full list is here ( the page refers to AngularJS-based extensions, but this is general, the same events is used for all visualizations, whether they are extensions or not)
So, to sum this up, you can get the title of the visualization like this:
You find a full working example in the Github repository.
This post is based on a session I originally held at Qonnections 2015, under the title ‘5 things you did not know you can do with the Mashup API’.