2014-09-22 45 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 ; 
});