简体   繁体   English

Backbone绑定模型查看

[英]Backbone bind Model to View

I'm currently learning how to deal with backbone.js. 我目前正在学习如何处理backbone.js。 Currently I have the problem that the model isnt updating when the view does through user input. 目前我遇到的问题是,当视图通过用户输入时,模型不会更新。

When I change the input of the textarea and I click the button, I send the old data since the view didnt notify the model that a change happened. 当我更改textarea的输入并单击按钮时,我发送旧数据,因为视图没有通知模型发生了更改。

What do I do wrong? 我做错了什么?

PostView

define([
    'jquery',
    'underscore',
    'backbone',
    'postModel'
], function($, _, Backbone, PostModel){

    var PostView = Backbone.View.extend({
        timer: null,

        el: $('.admin__wrapper'),
        template: $('#post_template'),

        initialize: function () {
            console.log('Initializing Post View');
            this.model = new PostModel({id: this.id});
            this.listenTo(this.model, 'change', this.render);
            this.model.fetch();
        },

        events: {
            'keyup textarea': 'keyup',
            'click button': 'submit'
        },

        submit: function(event) {
            this.model.save();
        },

        render: function(){
            var template = _.template(this.template.html(), { post: this.model });
            this.$el.html(template);

            return this;
        }
    });

    return PostView;
});

PostTemplate PostTemplate

<script type="text/template" id="post_template">
    <div class="post__title">
        <div class="entry">
            <input class="input--less" name="title" type="text" value="<%= post.get('title') %>" placeholder="Post title">
        </div>
    </div>

    <div class="entry post__panel post__markdown">
        <header class="entry__header post__panel__header">
            <span><small>markdown</small></span>    
        </header>

        <div class="post__boundary">
            <textarea name="content" id="editor" class="input--less post__editor" rows="5"><%= post.get('content') %></textarea>
        </div>
    </div>

    <div class="entry post__panel post__html">
        <header class="entry__header post__panel__header">
            <span><small>preview</small></span> 
            <span id="reading-time" class="float--right" style="text-align: right"></span>  
        </header>

        <div class="post__boundary">
            <div class="post__preview" id="preview"><%= post.get('content_compiled') %></div>
        </div>
    </div>

    <div class="post__footer">
        <span class="batch--tag-4 post__icon"></span>
        <button class="button button--blue post__button">Publish</button>
        <a href="#?" class="batch--settings-2 post__settings post__icon"></a>
    </div>
</script>

When the user clicks submit your not getting the data from the view you are simply saving the model with defaults, therefor not firing any change events, you will have to do something like this in keyup method which is triggered by the keyup event . 当用户点击提交你没有从视图中获取数据时,你只是使用默认值保存模型,因此不会触发任何更改事件,你必须在keyup方法中执行类似这样的事情,这是由keyup event触发的。

keyup: function(e)
{
    this.model.set(e.target.name, e.target.value, {silent: true});
}

Assuming you correctly named your textarea, the correct model attribute will be updated. 假设您正确命名了textarea,将更新正确的模型属性。 So now when this.model.save is called by submit these attributes will be saved triggering the change event and re-rendering the view. 所以现在当通过submit调用this.model.save时,这些属性将被保存,触发change事件并重新呈现视图。

当您更改页面中的表单时,此更改不会自动在模型中设置,您必须自己设置。

Backbone does not provide 2 way data binding like Angular Js, you need to set the model yourself and then sync the model. Backbone不像Angular Js那样提供双向数据绑定,您需要自己设置模型然后同步模型。

Try Backbone Mutators for 2 way data binding in Bakcbone. 尝试使用Backbone Mutators在Bakcbone中进行双向数据绑定。 Also as a follow up read this article 另外作为后续阅读本文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM