How to return the $tm1Ui data to function?

tm1

#1

I created a function which I use for transforming data into $tm1Ui.cellsetGet() format
i.e [{instance:'dev', cube:'System Info', cubeElements:['Server Time', 'String']}]

//Function $scope.CustomChartDataGet = function(instanceVar, cubeVar, elementListVar, elementVar){ var dataVar = []; for(i=0;i<elementListVar.length;i++){ dataVar[i] ={}; dataVar[i].instance = instanceVar; dataVar[i].cube = instanceVar; dataVar[i].cubeElements = []; dataVar[i].cubeElements[0]= elementListVar[i].key; dataVar[i].cubeElements[1]= elementVar; } return dataVar; };

it works fine. but I was trying to put $tm1Ui service inside, it looks I really cannot return $tm1Ui data into a function, because it looks $tm1Ui creates some kind local $scope, which cannot be retrieved outside into parent function CustomChartDataGet

//example of parameters //instanceVar = 'dev'; cubeVar ='Some Cube' , elementListVar = [{key:'A'},{key:'B'}], elementVar = 'Some Measure'; // $scope.CustomChartDataGet = function(instanceVar, cubeVar, elementListVar, elementVar){ var dataVar = []; for(i=0;i<elementListVar.length;i++){ dataVar[i] ={}; dataVar[i].instance = instanceVar; dataVar[i].cube = instanceVar; dataVar[i].cubeElements = []; dataVar[i].cubeElements[0]= elementListVar[i].key; dataVar[i].cubeElements[1]= elementVar; } $tm1Ui.cellsetGet(dataVar).then(function(data){ //console.debug('Returned by this function - %o', data); //$scope.CubeDataResult = data; return data; }); };
I also suspect that local variable dataVar cannot be passed into $tm1Ui in that way.


#2

Hi @eugene,

Any error being returned in the console?

Try also to examine / check the structure of the content of dataVar variable via console.debug('dataVar %o', dataVar); to see if you have the correct structure.

Another way of debugging / testing is to hard code the values first, then change them one by one until you arrive at the bug.

Cheers!
Paul


#3

Hi @plim,

<!-- HTML -->
{{CustomChartDataGet100(instanceX, cubeX, elementListX, elementX)}}
<BR> {{CubeDataResult100}}

Result on page:

//JS
$scope.CustomChartDataGet100 = function(instanceVar, cubeVar, elementListVar, elementVar){
  var dataVar = [];
  for(i=0;i<elementListVar.length;i++){
    dataVar[i] ={};
    dataVar[i].instance = instanceVar;
    dataVar[i].cube = instanceVar;
    dataVar[i].cubeElements = [];
    dataVar[i].cubeElements[0]= elementListVar[i].key;
    dataVar[i].cubeElements[1]= elementVar;
  }
      $tm1Ui.cellsetGet(dataVar).then(function(data){
        $scope.CubeDataResult100 = data;
      });

  return dataVar;
};

#4

oh. it looks my query is wrong…

dataVar[i].instance = instanceVar;
    dataVar[i].cube = instanceVar;

#5

Hi @eugene,

Looking at the JSON returned, the cube named passed was ‘dev’. Is that the correct name?

Cheers!
Paul


#6

Hi Paul,

it looks I can get the data though the scope:


But Quesstion was really about how to do it in more elegant way, i.e. returning the data to function directly. …
And it looks now I have an another issue - after getting the cube data though that function, my application become a bit dead. :sweat: So I am going through the all Nine Circles of the Hell:imp:


#7

Hi @eugene,

It really depends on what do you want to do. Are you planning to use for a chart? If so, you can use another variable. let us say, $scope.isDataReady = false; then set it to true only after you have the data available already.

In your HTML file, you can use an attribute like: <div ng-if="isDataReady"> ... you chart here ... </div>

Is the above what you are looking for?


Paul


#8

Hi @plim,

Actually you are right!) I’m going to use custom chart. :sunglasses:
Yeah, I understand what you are saying, I can use some kind ng-hide/show mechanism, I just looked for the best way to transform data.

i.e.

$scope.someFunction(someParameters){
   //some logic
  return someThing;
};

<!-- and later use this function on html -->

{{someFunction(someParameters)}} 

#9

Hi @eugene,

Transforming data will really depend on what is the final form. And there will be a few ways to achieving that.

One thing though, be careful or better avoid calling function to get data by just pasting it into an HTML file for the function to be called like:{{getMeDataFunction(params)}}So it is either you call them up first in your controller, or associate them with a button for the user to click.

In the above code you have, it should be more advisable to have a line at the end of your controller like to call your function there:

$scope.isDataReady = false;
$scope.someFunction(someParameters){
  
  // some logic

  // then set the ready flag
  $scope.isDataReady = true;

  return someThing;
};

// finally, call the function
$scope.someFunction();

Then in your HTML, use the ng-if whenever possible for performance consideration instead of ng-hide/ng-show.

As lastly, as a guide, remember that the JS / Controller script is loaded first before its HTML file.

Cheers!
Paul


#10

Hi @eugene,

Just to be clear on why your initial code wasn’t working is because you tried to return the data from a function that is returning a promise. A promise is asynchronous so the function (promise) returns after the containing function is completed. You are also calling return inside a function inside another function. That won’t work so as Paul has noted you should set the returned data to a variable in $scope and in your HTML check that it is not null before showing the chart.


#11

Hi @tryan,

I figured out what “promise” was on weekend. Later I will create a post about it, because it is not straightforward. Getting data from cube using $tm1Ui.cellsetGet() service, I had few subsequent challenges, so I would be really happy to see the Best Practice (i.e. Canvas for Tm1 Very Deep Dive) for $tm1Ui.