首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Backbone-带自定义灰尘模板的表单

Backbone-带自定义灰尘模板的表单
EN

Stack Overflow用户
提问于 2016-12-29 19:55:55
回答 1查看 97关注 0票数 0

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

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

// Index.jx

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
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;

我也尝试在添加元素后渲染表单,但没有成功。

代码语言:javascript
复制
$('#userInfoForm').append(this.profileForm.render()el);

每次都会出现不同的错误,但大多数都是因为在render()中调用时this.profileForm是未定义的,而根据我的理解,this.profileForm是未定义的,因为this.template无效

有什么办法可以正确的和backbone-forms沟通吗?

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

EN

回答 1

Stack Overflow用户

发布于 2017-01-03 14:42:34

不要试图从DOM中获取模板。你身上有灰尘。

代码语言:javascript
复制
this.profileForm = new Backbone.Form({
  template: require('./profile-form.dust'),
  model: this.model,
  validate: true
});

还有..。不要手动追加视图。backbone-form是一个backbone视图。所以只需创建它,向您的布局添加一个区域,然后

代码语言:javascript
复制
this.getRegion('formRegion').show(this.profileForm);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41379229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档