S
S
Stanislav Pochepko2015-08-11 15:56:36
backbone.js
Stanislav Pochepko, 2015-08-11 15:56:36

How to properly store templates for Backbone models?

I am puzzled by the question of how to properly organize the loading of templates for backbone models. I saw an example when templates are pulled from the server in the form of statically .html pages by Ajax, and the URL path where this template lies is simply stored in the model. Is this the right way? Can you share how you store your templates?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2015-08-11
@k12th

Well, for starters, storing templates in models is generally wrong. Only views should know about their templates.
If your assembly is organized using browserify, then templates can be compiled on the fly during assembly and in views you can simply write something like this:

render: () {
    this.$el.html(require('templates/template.jade')())
}

A
Alexey Ukolov, 2015-08-11
@alexey-m-ukolov

As with everything in programming, there is no "right" or "wrong" way here. It all depends on your tasks, each method has strengths and weaknesses depending on them.
I collect them into a variable using grunt and load the js file with them when the page starts (this is in development mode, of course, in production all scripts are collected in one file).
This allows you to compile on the server, that is, templates work faster (the question is how muchfaster they work, stays open). With this approach, of course, all templates are loaded in general, some of them may not be useful at all, to avoid this, they are loaded on demand when they are really needed. But this adds an element of asynchrony - you need to handle the loading of the template, show the spinner in the interface, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question