trapping model:destroy on the model list

by mark69_fnd

JavaScript

YUI({filter: 'raw',
  debug: true,
  combine: false}).use('node', 'model', 'model-list', function(Y) {
    var MyModel, m, lists, myList1 = new Y.ModelList({model: MyModel}), myList2 = new Y.ModelList({model: MyModel});
    
    MyModel = Y.Base.create('myModel', Y.Model, [], {
        initializer: function() {
            this.publish('destroyedOnServer', {emitFacade: true});
        }            
    }, {});
    
    myList1.add({
        a: 1
    });
    myList1.add({
        a: 2
    });
    myList1.add({
        a: 3
    });
    
    
    myList2.add(myList1);
    
    myList1.on(['*:destroy', '*:destroyedOnServer'], function(e) {
        Y.one('body').append('myList1: ' + e.type + '<br/>');
    });
    myList2.on(['*:destroy', '*:destroyedOnServer'], function(e) {
        Y.one('body').append('myList2: ' + e.type + '<br/>');
    });
    
    m = myList1.item(0);
    lists = m.lists.concat();
    m.destroy({ remove: true });
      lists.forEach(function (l) { m.addTarget(l); });
    m.fire('destroyedOnServer');
      lists.forEach(function (l) { m.removeTarget(l); });
    m.fire('destroyedOnServer');
    
    m = myList1.item(0);
    m.destroy();
});