Variable visibility in events and helpers

Hi,

I have a variable that represents a connection to another reporting system. how can I ensure that this var is visible in my helpers and events. I now create this var inside my onrendered function. But if I want to use it in my helper it says undefined. I tried to create a var on file level but this does not work: > Exception in template helper: ReferenceError: qlik is not defined

`var config = {
host: window.location.hostname,
prefix: “/”,
port: 80,
isSecure: window.location.protocol === “https:”
};

Template.multipleDivs.helpers({
appList: function () {
// …
var list = qlik.getAppList(function(list){}, config);
console.log(list)
return list;
}
});

Template.multipleDivs.onRendered(function() {

require.config( {
	baseUrl: ( config.isSecure ? "https://" : "http://" ) + config.host + (config.port ? ":" + config.port: "") + config.prefix + "resources"
} );

require( ["js/qlik"], function ( q ) {
	qlik = q;
	qlik.setOnError( function ( error ) {
		alert( error.message );
	} );

//open apps -- inserted here --
var app = qlik.openApp('debd03a3-a2a9-41cc-901c-d21b44279783', config);


//get objects -- inserted here --

app.getObject('QV04','grmSd');	
app.getObject('QV02','GdjmWq');	
app.getObject('CurrentSelections','CurrentSelections');
app.getObject('QV01','JfpX');	
app.getObject('QV03','Qjndk');

} );

});`

If you put your config in the onCreated callback:

Template.multipleDivs.onCreated(function() {
  this.config = {
    host: window.location.hostname,
    prefix: "/",
    port: 80,
    isSecure: window.location.protocol === "https:"
  };
});

Then you can use in your helper like this:

Template.multipleDivs.helpers({
  appList: function () {
    var config = Template.instance().config;
    // ...
    var list = qlik.getAppList(function(list){}, config);
    console.log(list)
    return list;
  }
});
2 Likes

Thank you. Yeah the Qlik object you mean? The config object was already available (at least in the oncreated function)

So the .js file does Not really behave like a module? I can create a var on file level?

Verstuurd vanaf mijn iPhone