Using lawnchair with Typescript
Asked Answered
E

2

3

Got a simple problem, which was touched upon in a similar question around the "this" scope with typescript.

I have a storage service which wraps lawnchair for my local storage concerns. Now I am migrating this code over to Typescript I have come to an issue I cannot solve.

public GetAll(callback: Function) {
           Lawnchair({ name: this.storeName }, function () {
                this.all(callback);
            });
        };

Now in the example above lawnchair makes this become the actual store you are actioning, but Typescript will make it become the instance of the class you are using. So is there a way around this issue?

== Edit ==

Updated the question example slightly as to not confuse with the underlying lawnchair method and the surrounding classes method.

Here is the error I get when trying to compile:

The property 'all' does not exist on value of type 'LawnchairStorageService'

Which is correct, that method does NOT exist on the containing class instance, it exists within the context of the lawnchair closure scopes this.

Ezzo answered 28/3, 2013 at 10:50 Comment(5)
Is this a problem with the generated code (run time), or with TypeScript not understanding what type 'this' is (compile time)? If it's the former attaching the generated code and saying what's wrong with it is helpful. If it's the latter casting 'this' to what you know it will be should do it.Diversification
It is the latter, the typescript compiler throws an error, and I dont think casting this would work as it is still going to be the underlying class instance not the lawnchair instance. I will update the question with the error though!Ezzo
Does this work: typescriptlang.org/Playground/…Mercuric
@jcfx Yep that compiles, I haven't got the application into a working state yet so I can test the compiled typescript but it should work at runtime as it will just wrap up the underlying self executed code.Ezzo
Good stuff. I think Ryan or Steve may be able to weigh in with a more elegant solution, but if that gets you moving forward then great. Let me know if it actually runs, and if it does I'll write it up as an answer.Mercuric
M
1

As noted in the comments above, this compiles, and I think it ought to run:

public GetAll(callback: Function) {
    return function (callback){
        Lawnchair({ name: this.storeName }, function () {
            this.all(callback);
        });
    }(callback);
};

... but I suspect someone else may be able to weigh in with a more elegant solution.

Mercuric answered 28/3, 2013 at 16:7 Comment(1)
You can have the answer as it works and if a better solution does come up I will move the answer, but you will hopefully get a few upvotes either way :)Ezzo
D
0

Have you tried the following :

public Exists(uniqueIdentifier: string, callback: Function) {
           var me = this;
           Lawnchair({ name: this.storeName }, function (me) {
                me.exists(uniqueIdentifier, callback);
            });
        };
Dunk answered 28/3, 2013 at 14:6 Comment(1)
that would not work, as your me variable would be equal to the instance of the containing class not the lawnchair instance created. So you are basically doing the exact same logic as shown in the question example but in a more verbose way.Ezzo

© 2022 - 2024 — McMap. All rights reserved.