我有问题了解如何使用dust.js模板显示自定义的骨干形式。
我代码的相关部分在这里。
//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);
错误每次都不同,但大多数是因为this.profileForm
在render()
中被调用时不确定,并且从我的理解中,this.profileForm
是不确定的,因为this.template
无效
有什么想法如何与骨干形式正确通信?
codepen(不起作用,而是显示更好的代码)
不要尝试从dom中获取模板。
this.profileForm = new Backbone.Form({
template: require('./profile-form.dust'),
model: this.model,
validate: true
});
也..不要手动附加视图..骨干形是一个骨干视图。.因此,只需创建它,在布局中添加一个区域,然后
this.getRegion('formRegion').show(this.profileForm);