The below code doesn't work because at the time the function is called this=window. I was expecting this = controller.actions.project but have since learned more about how this keyword works and now understand why that's not the case.
BROKEN
controller.actions = {
project: new TableauAction("Project",
function () {
$http.get('/reports/projects/').then(function (response) {
this.options = response.data;
});
}};
The following solves the problem, but it is quite inelegant
WORKS
controller.actions = {
project: new TableauAction("Project",
function () {
var self = controller.actions.project;
$http.get('/reports/projects/').then(function (response) {
self.options = response.data;
});
}};
TableauAction object:
function TableauAction(name, onChange) {
this.name = name;
this.onChange = onChange;}
My question is whether there is a more elegant way to access properties of the object from a function which is passed into it's constructor?