G_S
G_S

Reputation: 7110

Components vs templates in knockoutJS?

Question is about Components and templates. I am seeing components also does the same work as templates except that it also includes Viewmodel concept and using custom HTML tags. Other than these can someone help me in understanding if there are any differences and when to use components vs templates?

Upvotes: 1

Views: 621

Answers (1)

janfoeh
janfoeh

Reputation: 10328

For the most part, a component is a nice way to package a repeatedly used element or part of your project that has state of its own. This can of course also be done using templates and standalone viewmodels; components offer a way to group both parts together and thus help with organizing your code.

It gets interesting with the addition of $componentTemplateNodes in Knockout 3.3: now, your components can wrap child markup such as

<modal-dialog>
  <main>
   Are you sure you want to quit?
  </main>
  <menu>
    <button>cancel</button>
    <button>ok, quit</button>
  </menu>
</modal-dialog>

which is something that cannot be done easily and elegantly with traditional templates.

Upvotes: 2

Related Questions