This is a migrated thread and some comments may be shown as answers.

Custom Binders - constructor vs init

1 Answer 268 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Justin
Top achievements
Rank 1
Justin asked on 29 Jan 2017, 05:24 PM

I've been working with adding some custom binders, via typescript, to our project. All of the examples show that overriding the 'init' function is how I am supposed to parse attributes from the elements into (e.g.) custom properties. The issue I'm having is that the overridden 'init' function *never* seems to be called. If I switch to overriding the constructor, instead of init, this works as expected.

My question is - am I doing this incorrectly, is there anything inherently wrong with overriding the constructor itself, and *why* doesn't the override on init actually fire? I've tried this several ways, at this point I'm exactly duplicating the structure from kendo.d.ts, going with the assumption that the override might not fire if typescript doesn't see an exact match.

Again, note that the below extension is working - BUT if I were to remove the constructor override, the init override would never be called (confirmed via multiple debugging attempts).

module kendo.data.binders {
    export class date extends kendo.data.Binder {
        dateformat: string = "d";
 
        constructor(element: any, bindings: Bindings, options?: BinderOptions) {
            //call the base constructor
            super(element, bindings, options);
 
            this.dateformat = $(element).data("dateformat");
        }
 
        //Note: this NEVER seems to be called
        init(element: any, bindings: Bindings, options?: BinderOptions): void {
            //call the base constructor
            kendo.data.Binder.fn.init.call(this, element, bindings, options);
 
            this.dateformat = $(element).data("dateformat");
        }
 
        refresh() {
            var data = this.bindings["date"].get();
            if (data) {
                var dateObj = new Date(data);
                $(this.element).text(kendo.toString(dateObj, this.dateformat));
            }
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Dimiter Topalov
Telerik team
answered on 01 Feb 2017, 07:59 AM
Hello Justin,

In general the init() method will not be called automatically in this scenario. You can call it from the constructor. The call to the parent can be performed via super.init() in the init() method if needed, e.g.:

constructor() {
  ...
  this.init();
}
 
init() {
  super.init(...)
}

Calling super() and super.init() from the constructor is also an option, depending on the scenario:

http://docs.telerik.com/kendo-ui/third-party/typescript#use-kendo-ui-mvvm-in-typescript

I hope this helps, but if I am missing something, or you have further questions or issues, related to the topic, do not hesitate to contact us again.

Regards,
Dimiter Topalov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
MVVM
Asked by
Justin
Top achievements
Rank 1
Answers by
Dimiter Topalov
Telerik team
Share this question
or