这是我的代码:

    render: function () {
        var data = this.serializeData(),
            that = this;
        require(['text!trigger-menu/confirmation/' + that.model.get('type') + '-template.html'],
            function(templateHTML) {
               var html = _.template(templateHTML, data);
               that.$el.html(html);
        });
    }


我有3个可以显示的不同确认视图。 3个中的2个正在工作。一个不是这样的:

<ul>
    <% _.each(signupForms, function (signup) { %>
        <li><%- signup.name %></li>
    <% }); %>
    <% _.each(authorizedApps, function (app) { %>
        <li><%- app.name %></li>
    <% }); %>

    <% if ((!signupForms && !authorizedApps) || (!signupForms.length && !authorizedApps.length)) { %>
        <li>All signups</li>
    <% } %>
</ul>


错误是Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node': The new child element is null.

还有其他人遇到此错误吗?

最佳答案

您可以在初始化视图时执行以下操作,而不用在render函数中更改模板:

define([
    'jquery',
    'underscore',
    'backbone',
    'marionette',
    'text!templates/template1.html',
    'text!templates/template2.html',
], function ($, _, Backbone, Marionette, template1, template2) {
    var View = Backbone.Marionette.ItemView.extend({
        initialize: function () {
            if({YourConditionHere}){
this.template = Marionette.TemplateCache.get(template1);
        }else{
this.template = Marionette.TemplateCache.get(template2);
    });
    // Our module now returns our view
    return View ;
});

关于javascript - Marionette.js动态更改模板与requireJS不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25981935/

10-10 23:39