It. $ el.html against this. $ el.append

Is there any difference between this. $ el.html and that. $ el.append when rendering templates? I am completely new to js, ​​spine, etc. In the current project I'm working on, I see things like

this.$el.append(Project.Templates["template-library"](this.model)) 

in appearance. In this case, this template is intended for modal viewing. Then say that the modal view has a row for each item displayed in the modal view. Then, for each of these lines, the template receives a visualization as follows:

 this.$el.html(this.template({ libraries: libraries.toJSON() })); 

Is there a difference between the two? And why append() should be used in certain situations, and html() in another.

+4
source share
3 answers

For me, it really comes down to how you use the views < render method.

Some people like to use render as an initialize extension, because they only use it once when the view first appears on the page and often calls it from initialize . With this style, you can safely use append without worrying about accidentally adding items twice, because the render will not run twice.

Alternatively, you can use render to use again and again, whenever the view element needs to change in some way. The highway supports this style, for example. this.model.on('change', this.render, this); . For this style, append will be annoying, as you constantly need to check if elements exist before the append in them. Instead, html makes more sense as it wipes away everything that was before.

+3
source

With append, a new element will be inserted into $el , and html will change the contents of $el .

+1
source

Using .append() will allow you to add or add something to existing objects. Rather, using .html() , it will change the whole object to a new one.

+1
source

All Articles