Trunk style: how to deal with the el view element

I find in several reference examples different ways to do the same

I just would like to know if any of them are better, and in case they are exactly the same, which is more acceptable style

$('#header').html(new HeaderView().render());

against

new HeaderView({el:$('#header')).render();

and

this.$el.html( this.template() );

against

$(this.el).html( this.template() );

and finitely

render: function() {
    [...]
    return this.el;
}

against

render: function() {
    [...]
    return this;
}
+5
source share
1 answer

Send or not send, elin the constructor

I think it is always possible that it is better to send elin the constructor, so the JS logic will be more decoupled from the DOM structure, and not any DOM element name is hardcoded in the JS code.

, DOM , DOM , DOM DOM.

.

this.$el

, :

this.$el

VS

$(this.el)

- , . jQuery , DOM DOM jQuery, this.$el, .

render()

@muistooshort, this. , render() , , el, el , this.

, :

$('#header').html(new HeaderView().render()) 

:

$('#header').html(new HeaderView().render().el)
+5

All Articles