D
D
Dokma2017-07-28 13:16:32
JavaScript
Dokma, 2017-07-28 13:16:32

How to make the same data in different Views in the backbone?

I have a View with a header and a model with data to it, I also have a View with a rate block and a model to it too. Both of them have user data and their own data, I don’t understand how to synchronize user data with them. So far I have made the window.user variable and from there I take the data into the model of each View. But if you change the variable, everything in the View is not updated. Tell me what's right

var headerView;
$(function () {
    var Header = Backbone.Model.extend({
        defaults: {
            online: 0,
            user: false
        },
        initialize: function () {
            console.log('rt');
        },
    });

    var HeaderView = Backbone.View.extend({
        model: new Header({
            user: window.user
        }),
        template: _.template($('#header-m-template').html()),
        initialize: function () {
            console.log(this.model.toJSON());
            this.listenTo(this.model, 'change', this.render);
            this.render();
        },
        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });

   headerView = new HeaderView({
       el: $('.header-m')
   });
});

$(function () {
    var BetInput = Backbone.Model.extend({
        defaults: {
            bet: 0,
            user: false
        },
        validate: function (args) {
            if(parseInt(args.bet) < 0) {
                return 'Ставка должна быть больше 0';
            }
        }
    });

    var BetInputView = Backbone.View.extend({
        model: new BetInput({
            user: window.user
        }),
        template: _.template($('#bet-input-template').html()),
        initialize: function () {
            console.log(this.model.toJSON());
            this.listenTo(this.model, 'change', this.render);
            this.render();
        },
        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    })
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question