Change in event in the relational base of the spine does not work

I work with a relational database with HasMany relationships. The add event will be fired, but the change event will not.

Model1 = Backbone.RelationalModel.extend({
    urlRoot: '/Leve1/',

    initialize: function () {
        this.listenTo(this, 'add:relatedModels', this.relationModelAdd);
        this.listenTo(this, 'change:relatedModels', this.relationModelChange);
    },

    relations: [{
        type: Backbone.HasMany,
        key: 'relatedModels',
        relatedModel: 'InnerModel'
    }],

    relationModelAdd: function () {
        alert("Add in related model");
    },

    relationModelChange: function () {
        alert("Change in related model");
    }
});

InnerModel = Backbone.RelationalModel.extend({
    urlRoot: '/Level2/'         
});

var outerModel = new Model1({
    id: 'model1',
});
var innerModel = new InnerModel({
    id: 'model11',          
});

outerModel.get('relatedModels').add(innerModel);

innerModel.set({
    id: 'model13'
});

Change an event using a HasOne relationship, but not with HasMany.

+4
source share
1 answer

It seems that the event changedoes not fire in the external model Model1if the change occurs in any model InnerModelwithin the HasMany relationship collection. See Question in Backbone.RelationalModel on GitHub: Changing events that do not fire when a collection changes . The question was closed by the author, who said:

, change , - , Backbone.RelationalModel, this._relations . , :

Backbone.RelationalModel.prototype.bindRelationEvents = function() {
    var dit = this;
    _.each( this.getRelations(), function( relation ) {
        relation.related && relation.related.bind( 'all', function() {
            dit.trigger.apply( dit, arguments );
            // Or even simply `dit.render()`, if the number of events is reasonable...
        } );
    });
};

this.bindRelationEvents , .

+4

Source: https://habr.com/ru/post/1531770/


All Articles