2016-12-29 71 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);