Nam Ngo
Nam Ngo

Reputation: 263

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.

Upvotes: 3

Views: 371

Answers (3)

Johnny
Johnny

Reputation: 7351

super(_.extend(options, {className: 'col-md-6', tagName: 'div'}));

Upvotes: 0

thoughtrepo
thoughtrepo

Reputation: 8383

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

Upvotes: 2

jlujan
jlujan

Reputation: 1188

tagName is a member of the options argument. Can pass it on view creation.

var myView = new MyView({tagName: "li"})

Upvotes: 1

Related Questions