2016-12-29 84 views
0

我在理解如何使用Dust.js template顯示customized backbone-forms時遇到問題。自定義灰塵模板的骨幹形式

我的代碼的相關部分在這裏。

// Index.jx

<head> 
    [...] 
    <script type="text/javascript" src="scripts/jquery.js"></script> 
    <script type="text/javascript" src="scripts/underscore.js"></script> 
    <script type="text/javascript" src="scripts/backbone.js"></script> 
    <script type="text/javascript" src="scripts/backbone-forms.js"></script> 

</head> 

// dashboard.dust

<div id="placeholderForm"></div 
<section id="formTest"> 
      <form> 
        <h1>My Title here</h1> 
        <div data-editor="firstname,lastname,birthday"></div> 
        <hr> 
        <p> 
        some info 
        <div data-fields="address"></div> 
        </p> 
       </form> 
       </section> 

// DahsboardView.js

const 
    UserModel = require('../../models/user-model'), 
    _ = require('underscore'); 

let View = Backbone.Marionette.LayoutView.extend({ 
    template: require('./profile-dashboard.dust'), 


    initialize: function() { 
     let self = this; 
     _.bindAll(this, "render"); 

     this.model.bind('change', this.render); 
     this.model.fetch({ reset: true }); 
     console.log('model', this.model); 


    this.profileForm = new Backbone.Form({ 
     template: this.template, 
     model: this.model, 
     validate: true 
    }); 
     /* 
     this.profileForm = new Backbone.Form({ 
     model: this.model, 
     //template: _.template($('#formTest').html), //this.template, 
     validate: true 
     }).render(); 
     */ 
    }, 

    onRender: function() { 
     console.log('render init'); 
     console.log('form', this.profileForm); 
     let self = this; 
     this.profileForm.render(); // Got problem here because template is not recognise as function or generally just not recognise as valid template 
     $('#placeholder').append(this.profileForm.el); 
     return this; 
    } 

}); 

module.exports = View; 

我也嘗試追加後呈現形式元素,但沒有運氣。

$('#userInfoForm').append(this.profileForm.render()el); 

錯誤是不同的每一次,但在render(),並從我的理解調用時大多都來,因爲this.profileForm是不確定的this.profileForm是不確定的,因爲this.template無效

任何想法如何正確地與骨幹-形式溝通?

Codepen(沒有工作,但表現出更好的代碼)

+1

包括[MCVE ]。 –

+0

感謝您的評論@EmileBergeron。我在View中添加了所有相關部分,並且CodePen無法正常工作,但至少會顯示組織爲我的代碼。 –

回答

0

不要嘗試,並從DOM模板..你有灰塵。

this.profileForm = new Backbone.Form({ 
    template: require('./profile-form.dust'), 
    model: this.model, 
    validate: true 
}); 

也..不要手動添加視圖..骨幹形式是骨幹觀點..所以只是創建它,一個區域添加到您的佈局和

this.getRegion('formRegion').show(this.profileForm);