Backbone bind Model to View

Itrulia

I'm currently learning how to deal with 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.

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

<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>
Kyle Needham

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: 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. So now when this.model.save is called by submit these attributes will be saved triggering the change event and re-rendering the view.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bind Model to a View asynchronously in Backbone

From Dev

Backbone bind model attribute to input

From Dev

passing model object to view in backbone

From Dev

Backbone View says model is undefined?

From Dev

Backbone fetch model then render view

From Dev

Backbone view not listeningTo a model change

From Dev

passing model object to view in backbone

From Dev

Bind a Backbone Model attribute to a TinyMCE text

From Dev

How to bind/unbind window events in backbone view

From Dev

Backbone bind event to model. Bind if value equal to parameter

From Dev

How to create / update a Backbone View based on a response from a Backbone Model?

From Dev

Listen in Backbone view to nested Backbone Relational model events

From Dev

Backbone partial view not rendering the latest model

From Dev

Update my view on a change in model with backbone

From Dev

Backbone model change and view render update

From Dev

Backbone view render with multiple model fetch

From Dev

require. how to pass a model to a view (backbone)

From Dev

Render a View of Backbone Model returns undefined

From Dev

Backbone View receiving regular object instead of model

From Dev

Update my view on a change in model with backbone

From Dev

Backbone model change and view render update

From Dev

changing view's model in backbone.js

From Dev

Backbone view listening to model, not firing on change or reset?

From Dev

Bind selected item to view model

From Dev

Unable to model bind a collection within a view model

From Dev

Backbone Marionette : Add a model (and render its view) to a nested Composite View

From Dev

Backbone Marionette : Add a model (and render its view) to a nested Composite View

From Dev

WPF bind IsEnabled to method on view model

From Dev

WPF MVVM bind Hyperlink RequestNavigate to View model

Related Related

  1. 1

    Bind Model to a View asynchronously in Backbone

  2. 2

    Backbone bind model attribute to input

  3. 3

    passing model object to view in backbone

  4. 4

    Backbone View says model is undefined?

  5. 5

    Backbone fetch model then render view

  6. 6

    Backbone view not listeningTo a model change

  7. 7

    passing model object to view in backbone

  8. 8

    Bind a Backbone Model attribute to a TinyMCE text

  9. 9

    How to bind/unbind window events in backbone view

  10. 10

    Backbone bind event to model. Bind if value equal to parameter

  11. 11

    How to create / update a Backbone View based on a response from a Backbone Model?

  12. 12

    Listen in Backbone view to nested Backbone Relational model events

  13. 13

    Backbone partial view not rendering the latest model

  14. 14

    Update my view on a change in model with backbone

  15. 15

    Backbone model change and view render update

  16. 16

    Backbone view render with multiple model fetch

  17. 17

    require. how to pass a model to a view (backbone)

  18. 18

    Render a View of Backbone Model returns undefined

  19. 19

    Backbone View receiving regular object instead of model

  20. 20

    Update my view on a change in model with backbone

  21. 21

    Backbone model change and view render update

  22. 22

    changing view's model in backbone.js

  23. 23

    Backbone view listening to model, not firing on change or reset?

  24. 24

    Bind selected item to view model

  25. 25

    Unable to model bind a collection within a view model

  26. 26

    Backbone Marionette : Add a model (and render its view) to a nested Composite View

  27. 27

    Backbone Marionette : Add a model (and render its view) to a nested Composite View

  28. 28

    WPF bind IsEnabled to method on view model

  29. 29

    WPF MVVM bind Hyperlink RequestNavigate to View model

HotTag

Archive