Search code examples
backbone.jstypescript

Backbone Typescript can not set tagName


I am setting up Backbone.View with Typescript. I want to set tagName to li so I follow the TodoMVC example of typescript: https://github.com/tastejs/todomvc/blob/gh-pages/labs/architecture-examples/typescript-backbone/js/app.ts

Here is my code:

constructor(options?: any) {
    this.tagName = 'li';

    super(options);    
    this.template = AppData.template['Query/QueryItemViewTpl'];    
}

However, VS complains about the super call: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.

So, my question is how do I set the tagName before super call? Also, is there a general way/pattern that I can use to set up my class property before initialize it?

Thanks

Edit: I found this work-around:

initialize() {
    ...   
    this.setElement($("<li />"));    
    ...
}

I still want to know which is the best way to do this.


Solution

  • constructor(options: any = {}) {
        options.tagName = 'li';
        super(options);
    }
    

    If I'm not mistaken, you can do things before the super() call as long as it doesn't affect this