gpt4 book ai didi

javascript - Marionette.js 动态更改模板,requireJS 不起作用

转载 作者:行者123 更新时间:2023-11-28 07:56:58 25 4
gpt4 key购买 nike

这是我的代码:

    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 个可以显示的不同确认 View 。 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:无法在“Node”上执行“appendChild”:新的子元素为空。

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

最佳答案

您可以在初始化 View 时执行此操作,而不是在渲染函数中更改模板:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com