V
V
Vyacheslav2012-10-24 11:34:54
JavaScript
Vyacheslav, 2012-10-24 11:34:54

Backbone requirejs Templates after node + r.js optimization?

Hello. I continue my torment with the development of a small front-end application.
Now I ran into this problem:
Backbone + requirejs + plugin for templating !text
There are a lot of templates so I don't want to include them in the page !text handles it very well.
But here I decided to try to code all this with the help of node and r.js, after optimization, the templates also merge into one file.
Having rummaged in the settings, I found the necessary parameter that allows you not to include text dependencies:

//Inlines the text for any text! dependencies, to avoid the separate
    //async XMLHttpRequest calls to load those dependencies.
    inlineText: true,

Templates are no longer included in the shared file, but they don't work either: Error: Dynamic load not allowed.
build.js
{
    baseUrl: "f/app",
    appDir: "..",
    dir: "dist",
    modules: [
    {
        name: "catalog"
    }
    ],
    stubModules: ['text'],
    optimizeAllPluginResources: false,
    inlineText: false,
    paths: {
        app: '../libs',
        jquery: 'empty:',
        underscore: 'empty:',
        backbone: 'empty:',
        marionette: 'empty:',
        JSON: 'empty:',
        // Plugins
        text: '../libs/rjs-text',
    },
    exclude: ["jquery","underscore","backbone","marionette", 'JSON', 'text']
}

Nobody faced? Or what other means can be considered to still leave the possibility of editing templates, but also to optimize the script?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Petrychuk, 2012-10-24
@vermilion1

github.com/gruntjs/grunt-contrib-jst

var JST = window.JST = window.JST || {};
var fetchTemplate = function (name) {
    var path = 'app/templates/' + name + '.html';
    if (!JST[path]) {
        $.ajax({
            url: path,
            async: false
        })
        .then(function (contents) {
            JST[path] = _.template(contents);
        });
    }
    return JST[path];
};

During development - XHR. Release - templates are folded into one file using grunt-contrib-jst

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question