0

I use typescript with breeze. How can i pass a typescript function to executeQuery.then?

class MyClass{
 ...
    myFunc(data:any):void{
       ...
    }

    doQuery():void{
        var manager = new breeze.EntityManager('/breeze/dbentities');
        var query = breeze.EntityQuery.from("Corporations").where("Name", "startsWith", "Zen");
        manager.executeQuery(query)
               .then(this.myFunc);  // does not work!
    }
}

2 Answers 2

1

Use this.myFunc instead of myFunc.

It might be a context problem. Try this.myFunc.bind(this) instead of this.myFunc.


For more information about context, refer "this" and "Function.prototype.bind" article from MDN.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry forgotten to add this. But with this the function is also not called.
Then it might be context problem, try this.myFunc.bind(this).
where should i call the binding?
It binds function's execution context(a.k.a this) to this. I updated my answer, please see linked articles. These are definitely better than my poor explanation. Also note that it has no concern with TypeScript. :)
0

First, this is working perfectly in my own classes. What is "not working", what error message is thrown?

Second, to be sure that "this" is my typescript class context I always use a lambda like this:

doQuery(): void {
    ...
    manager.executeQuery(query).then((data: breeze.QueryResult) => {
        this.myFunc(data);
    });
}

In this case the TS compiler produces a "var _this = this" at the beginning of the doQuery function which is your class context and converts the "this.myFunc(data)" call to "_this.myFunc(data)".

And better use type declarations like "breeze.QueryResult" instead of any.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.