You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In knockout computed observables it is possible to change the value of "this" in the read/write callbacks by settings the owner option when defining the computed observable.
Something like this:
It would be nice with a similar definition in ko.command and ko.asyncCommand.
For example:
aCommand = ko.command({execute: a, owner: someObject});
This feature is particularly interesting when working with typescript. Here "this" is only defined in the constructor, so unless you want to write all command implementations in the constructor, it is needed to pass "this" from here to the command callback.
For example:
enter() {
"this" is now the viewmodel and not window.
}
The implementation is fairly simple. I have made it by including:
owner = options.owner || this,
in the top of the two command types, and then invoke the callbacks with this context with canExecuteDelegate.apply(owner, [self.isExecuting()]) and changed "this" to "owner" in executeDelegate.apply(owner, args).
The text was updated successfully, but these errors were encountered:
I tend to avoid the use owner (or this) in Knockout, it's a hassle keeping track that the context is always set properly.
In JavaScript I usually stick to the var self = this pattern but since almost always use CoffeeScript nowadays that is all solved with Fat Arrows. Do you know if TypeScript have anything similar?
Yes, there is something similar. You can write (in the typescript constructor):
var self = this;
aCommand = ko.command({execute: () => {
// () => {} makes "this" here that of the parent context. That is, "this === self" here.
}});
The trouble is that for this to work, the execute implementation must be in the view model constructor, which tends to get very long.
In the lambda-style implementation, you can of cause call other methods in the view model, but the parameter passing needed there becomes ugly (I think).
Feature request.
In knockout computed observables it is possible to change the value of "this" in the read/write callbacks by settings the owner option when defining the computed observable.
Something like this:
aComputed = ko.computed({read: getComputed, owner: someObject});
It would be nice with a similar definition in ko.command and ko.asyncCommand.
For example:
aCommand = ko.command({execute: a, owner: someObject});
This feature is particularly interesting when working with typescript. Here "this" is only defined in the constructor, so unless you want to write all command implementations in the constructor, it is needed to pass "this" from here to the command callback.
For example:
enterCommand;
constructor() {
this.enterCommand = ko.command({ execute: enter, owner: this });
}
enter() {
"this" is now the viewmodel and not window.
}
The implementation is fairly simple. I have made it by including:
owner = options.owner || this,
in the top of the two command types, and then invoke the callbacks with this context with canExecuteDelegate.apply(owner, [self.isExecuting()]) and changed "this" to "owner" in executeDelegate.apply(owner, args).
The text was updated successfully, but these errors were encountered: