I have the below TypeScript class.
export class BrandViewModel {
private _items = ko.observableArray();
public Add(id: number, name: string, active: boolean) : void {
this._items.push(new BrandItem(this, id, name, active));
}
public Get() : void {
$.get("/api/brand", function(items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
}, "json");
}
}
The resulting javascript for the Get
method is:
BrandViewModel.prototype.Get = function () {
$.get("/api/brand", function (items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
}, "json");
};
I have seen in the TypeScript
documentation that I can do this:
public Get() : void {
$.get("/api/brand", () => function(items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
}, "json");
}
Which results in the below, where _this
is now a reference to the BrandViewModel
instance but the this
inside the jquery .each
function is not changed to _this
as I might expect:
BrandViewModel.prototype.Get = function () {
var _this = this;
$.get("/api/brand", function () {
return function (items) {
$.each(items, function (i, item) {
this.Add(item.Id, item.Name, item.Active);
});
};
}, "json");
};
Instead I have done the below in TypeScript
:
public Get(): void {
var _this = this;
$.get("/api/brand", function(items) {
$.each(items, function (i, item) {
_this.Add(item.Id, item.Name, item.Active);
});
}, "json");
}
which gives me the result I wanted:
BrandViewModel.prototype.Get = function () {
var _this = this;
$.get("/api/brand", function (items) {
$.each(items, function (i, item) {
_this.Add(item.Id, item.Name, item.Active);
});
}, "json");
};
Does anyone know a more appropriate way to do this?
You can do this:
Which generates:
Consistent with ECMAScript 6 arrow functions, TypeScript lexically binds this when using =>.