2014-09-22 34 views
1

这里是我的代码:Marionette.js动态变化的模板,requireJS不工作

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.

其他人遇到这个错误?

+0

调试器已检查'变种HTML = _.template(templateHTML,数据);'线? 'templateHTML,data'中包含哪些数据以及调用_.template的结果? – aleha 2014-09-23 07:24:51

+0

您可能正在使用其他视图中存在的元素,但在这个视图中不存在。只是猜测。 – Yura 2014-09-23 12:37:34

+0

@dennismonsewicz,这个问题是关于requirejs的行为。在你的代码**中,$ el.html(html); **将在另一个线程运行,当浏览器是免费的,你的** templae **被加载。我希望别的地方你正在寻找渲染内容中的元素,但内容没有渲染。 – 2015-03-30 05:37:07

回答

0

而不是在渲染函数变化的模板,你可以做到这一点,当你初始化视图:

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 ; 
});