Reputation: 81
I have a big problem with sending data to my WorkspaceController.js from startingWebGazer service. I have a webgazer.js which evaluate prediction points and pass it to xprediction and yprediction variables in StartingWebgazer.js(it is my service)
app.service('startingWebGazer', function () {
this.startgazer = function () {
window.onload = function () {
webgazer.setRegression('ridge') /* currently must set regression and tracker */
.setTracker('clmtrackr')
.setGazeListener(function (data, elapsedTime) {
if (data == null) {
return
}
var xprediction = data.x; //these x coordinates are relative to the viewport
console.log(xprediction);
return xprediction;
// var yprediction = data.y; //these y coordinates are relative to the viewport
}).begin()
.showPredictionPoints(true); /* shows a square every 100 milliseconds where current prediction is */
var setup = function () {
//Set up the main canvas. The main canvas is used to calibrate the webgazer.
var canvas = document.getElementById("plotting_canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.position = 'fixed';
};
function checkIfReady() {
if (webgazer.isReady()) {
setup();
} else {
setTimeout(checkIfReady, 100);
}
}
setTimeout(checkIfReady, 100);
};
window.onbeforeunload = function () {
console.log('user want to left the page');
//webgazer.end(); //Uncomment if you want to save the data even if you reload the page.
window.localStorage.clear(); //Comment out if you want to save data across different sessions
}
}});
so, I want to pass xprediction to a WorkspaceController.js to do some evaluates in the future in the controller
app.controller('WorkspaceController', ['$scope', 'startingWebGazer', function ($scope, startingWebGazer) {
// create configuration object
$scope.startingWebGazer = startingWebGazer;
console.log($scope.startingWebGazer);
console.log('data in workspace', $scope.startingWebGazer.startgazer());}]);
but i have a big problem, because startgazer is undefined although xpreriction evaluate well(it is that console.log shows)
whhat is the problem? why I can see xprediction in console.log, but cant to pass it to the controller? and what I should to do?
Upvotes: 0
Views: 103
Reputation: 198
i'm not sure if i understand your problem correctly, but startgazer
does not return a value the way it is defined. it assigns window.onload
and window.onbeforeunload
and then finishes without a return statement, which means that it returns undefined
. you could consider assigning xprediction
to a variable in the service scope and return that variable. i could imagine that something like that could serve your purpose:
app.service('startingWebGazer', function () {
let x = 0;
this.startgazer = function () {
window.onload = function () {
...
x = xprediction;
...
}
...
}
this.getXPrediction = function() {
return x;
}
})
i think it would make sense to further refactor the code and e.g. call startgazer
in a run method or make getXPrediction
return a promise, but the example above could help you get started. hope it helps.
Upvotes: 1