【问题标题】:Backbone-forms with custom Dust template带有自定义 Dust 模板的主干表单
【发布时间】:2016-12-29 11:55:54
【问题描述】:

我在理解如何用Dust.js template 显示customized backbone-forms 时遇到问题。

我的代码的相关部分在这里。

// 索引.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.profileFormrender() 中调用时未定义,据我了解this.profileForm 未定义,因为this.template 无效

任何想法如何正确地与骨干形式通信?

Codepen(不工作但更好地显示代码)

【问题讨论】:

  • 感谢您的评论@EmileBergeron。我在我的视图中添加了所有相关部分,并且 codePen 无法正常工作,但至少显示了按我的方式组织的代码。

标签: javascript backbone.js marionette dust.js backbone-forms


【解决方案1】:

不要尝试从 DOM 中获取模板。你有灰尘。

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

另外..不要手动附加视图..主干形式是主干视图..所以只需创建它,在布局中添加一个区域,然后

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 2015-02-06
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    相关资源
    最近更新 更多