2

I have a TypeScript component with a JS function in it. The function is called by Kendo UI so the function is executed in Javascript context, which means I can't use "this" (this returns the caller Kendo component). I want to redirect the page in this function but I rather use router.navigate* functions instead of location.href replace. Is it possible to access router instance in Javascript context?

export class MyComponent implements AfterViewInit {
    //template: `<div #grid></div>`
    @ViewChild('grid') private grid: ElementRef;

    ngAfterViewInit() {
        this.configureGrid();
    }

    private configureGrid() {
        let kendoGrid: kendo.ui.Grid = jQuery(this.grid.nativeElement).data("kendoGrid");
        let kendoColumn: kendo.ui.GridColumn = {};
        kendoColumn.command = { text: "My Button", click: this.myButtonCommand, className: "btn" };
        kendoGrid.columns.add(kendoColumn);
    }

    myButtonCommand(e) {
        //redirect here
    }
}
4
  • 2
    Please show some code. Commented Apr 29, 2016 at 11:06
  • you can use var that = this; trick. google.com/… Commented Apr 29, 2016 at 11:09
  • 1
    Have you tried { text: "My Button", click: this.myButtonCommand.bind(this), className: "btn" };? Commented Apr 29, 2016 at 11:18
  • Added sample code, sorry about that. Commented Apr 29, 2016 at 11:19

1 Answer 1

1

I think this should work

private configureGrid() {
    let kendoGrid: kendo.ui.Grid = jQuery(this.grid.nativeElement).data("kendoGrid");
    let kendoColumn: kendo.ui.GridColumn = {};
    kendoColumn.command = { text: "My Button", click: (e) => this.myButtonCommand(e), className: "btn" };
    kendoGrid.columns.add(kendoColumn);
}

and you should be able to just call this.router.navigate...() in myButtonCommand()

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

1 Comment

Thank you. I also want to add that your "click: this.myButtonCommand.bind(this)" method works too.

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.