0

I have Backbone Model that collect data from server:

Job.Models.Response = Backbone.Model.extend({
    defaults: {
    'authStatus': false,
    'id': '1',
    'name': 'name',
    },

        urlRoot: '/static/js/public/json/'
    });

I have button with data-id = "id from /static/js/public/json/".

Job.Views.Response = Backbone.View.extend({
    el: '.ra-response-button',

    events: {
        "click": "load"
    },

    load: function () {

        var info = this.$el.data();

        this.model.set({ id: info.id});
        this.model.fetch();

        if (this.model.attributes.authStatus === false) {
            console.log('Register')
        }
        else {
            console.log('Unregister')
        }
    }

});

If i console.log my model after fetch, its dont update, but data fetch success. What kind of problem can be here?

Here i init our plugin:

var responseModel = new Job.Models.Response;
var response = new Job.Views.Response({ model: responseModel });
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

I resolve my problem. Finally View.

    Job.Views.Response = Backbone.View.extend({
    el: '.ra-response-button',

    events: {
        "click": "load"
    },

    load: function () {
        var that = this;

        var info = that.$el.data();

        that.model.set({ id: info.id});
        that.model.fetch({
            success: function() {
                if (that.model.attributes.authStatus === true) {
                    new Job.Views.ResponseForm({ model: that.model })
                }

                else {
                    new Job.Views.ResponseAuth({ model : that.model })
                }
            },

            error: function() {
                alert('Error, repeat please.')
            }
        });
    }
});
  • Glad you figured it out. You can always pass success and error callbacks to fetch() and save(), but an often more powerful way of attaching callbacks that are called when the fetch or save is complete is taking advantage of the returned Deferred objects. Both fetch() and save() return a jQuery deferred object allowing you to do something like `$.when(that.model.fetch()).done(function() { /* do something here */ });` -- Keep in mind that using $.when() allows you to pass as many deferred objects as you want to it and wait until they are all complete. I use it frequently with backbone. – Justin Warkentin Apr 11 '13 at 05:50