Access properties of javascript object from a function which is passed into it's constructor -
the below code doesn't work because @ time function called this=window. expecting this = controller.actions.project have since learned more how this keyword works , understand why that's not case.
broken
controller.actions = { project: new tableauaction("project", function () { $http.get('/reports/projects/').then(function (response) { this.options = response.data; }); }};
the following solves problem, 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 whether there more elegant way access properties of object function passed it's constructor?
add "this" context onchange
callback.
function tableauaction(name, onchange) { this.name = name; this.onchange = onchange; //add context onchange this.onchange.bind(this); }
then change following this:
controller.actions = { project: new tableauaction("project", function () { //"this" refers tableauaction instance var $this = this; $http.get('/reports/projects/').then(function (response) { //this refers $http callback response.. $this.options = response.data; }); }}; }
Comments
Post a Comment