Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change "this" in execute and canExecute callbacks #23

Open
rmja opened this issue Apr 28, 2013 · 3 comments
Open

Change "this" in execute and canExecute callbacks #23

rmja opened this issue Apr 28, 2013 · 3 comments

Comments

@rmja
Copy link

rmja commented Apr 28, 2013

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).

@hfjallemark
Copy link

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?

@rmja
Copy link
Author

rmja commented Apr 30, 2013

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).

@hfjallemark
Copy link

Alright, since Knockout computeds have it I think it can be a good option to have on our commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants