0

Dust.js templateを使用してカスタマイズされたバックボーン フォーム を表示する方法を理解するのに問題があります。

私のコードの関連部分はここにあります。

// 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>

// ダッシュボード.ダスト

<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 (動作していませんが、コードをよりよく表示するため)

4

1 に答える 1

0

DOM からテンプレートを取得しようとしないでください。ほこりがあります。

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

また..ビューを手動で追加しないでください..backbone-formはバックボーンビューです..それを作成し、レイアウトに領域を追加して、

this.getRegion('formRegion').show(this.profileForm);
于 2017-01-03T06:42:33.923 に答える