Marionette LayoutView removed my root HTML element in template

风流意气都作罢 提交于 2019-12-11 08:09:27

问题


This is my template file outerLayout.html:

<section id="index-wrapper">
    <navigation id="menu">menu1</navigation>
    <article id="content">main content</article>
    <footer id="footer">footer</footer>
</section>

This is my outerLayout.js

var $ = require('jquery');
var Backbone = require('backbone');
Backbone.$ = $;
var Maronette = require('backbone.marionette');
var compiledTpl = require('./outerLayout.html');
// console.info('compiledTpl({})', compiledTpl({}));
    console.info('compiledTpl({})', compiledTpl({}));
var OuterLayout = Backbone.Marionette.LayoutView.extend({
    template: compiledTpl({}), 
    el: '#main',
    regions: {
        menu: '#menu', 
        content: '#content', 
        footer: '#footer'
    }
});

module.exports = new OuterLayout();

Here are the code in router to render the layoutView:

var AppRouter = Backbone.Marionette.AppRouter.extend({
    routes : {
        '': 'index'
    }, 
    index : function () {
        var outerLayout = require('../layout/outerLayout/outerLayout');
        outerLayout.render();

    }
    }    
});

The render result is:

But the result should be this one:

In short words, the render function removed tag which is not my objective. How can I fix it?


回答1:


Problems comes from Marionette.TemplateCache.loadTemplate and Marionette.TemplateCache.compileTemplate.

As you can see from annotations of functions you need to override them in order to user other templates rather than Underscore's _.template().

And the actual line from definition of loadTemplate which is responsible for this behavior (your issue) is:

var template = Backbone.$(templateId).html();

cause, in case you didn't rewrited loadTemplate and compileTemplate, Marionette will assume that you using default underscore's templates (If you will try it with underscore it will work as expected).

Example:

If you using Handlebars for templates:

Marionette.TemplateCache.prototype.compileTemplate = function (yourTemplate) {
    if ($.isFunction(yourTemplate)) {
        return yourTemplate;
    } else {
        return Handlebars.compile(yourTemplate);
    }
};

Marionette.TemplateCache.prototype.loadTemplate = function (yourTemplateId) {

    var yourTemplate;

    if (Handlebars.templates && Handlebars.templates[yourTemplateId]) {
        // from cache
        yourTemplate = Handlebars.templates[yourTemplateId];
    } else {
        // load it here
    }
    return yourTemplate;
};

So just rewrite these methods and you ready to go!



来源:https://stackoverflow.com/questions/25590757/marionette-layoutview-removed-my-root-html-element-in-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!