Tried to minify my code so it is clear for you to see what i want to accomplish but not succeed.
What i want to accomplish is that when i do console.log($scope.tempData);
i see the data instead of undefined so i can use it in the controller. I allready know that promises are the way to go, because of my question asked here on stackoverflow: $Scope variable is undefined
But what i don't know is how i can get a promise from $scope.getCube();
so i can just do $scope.getCube().then(function (data) { ... });
i found this: how to create asynchronous function but the answer was not clear enough for me.
/////////// Side Question ////////////
The code i provide on the bottom is what i have found on Github, not everything of that code is clear for me: Why is ClientService.dynamic() a promise? There is no q.defer
in this.dynamic = function (params)
?
//////////////////////////////////////////////
for a better reading of the code: JSFiddle
// CONTROLLER
$scope.buildObject = function () {
$scope.getCube(); //
console.log($scope.tempData); //gives undefined, obvious because getCube is not done running yet.
}
$scope.getCube = function () {
promise = ClientService.dynamic(params);
promise.then(function (data) {
$scope.tempData = data[0];
return $scope.tempData;
}
}
/////////////////////////////////////////////////////
// DYNAMIC SERVICE
this.dynamic = function (params) {
return qvCommService.send(createHyperCube).then(function (data) {
}
}
//////////////////////////////////////////////////
// SEND SERVICE
send: function (msg) {
var deferred = $q.defer();
var promise = deferred.promise;
this.socket = new WebSocket(ws://*************);
this.socket.addEventListener('open', function (e) {
deferred.resolve("connected")
});
this.socket.send(angular.toJson(msg))
return promise;
}