Lee![]()
BaseRevive provides a non-destructive destroy by simply marking an object as destroyed but prevents events from being detached and any of the destructors from executing. Objects can be revived (undestroyed) by calling the revive() method provided by the extension.
Designed primarily as an extension for Y.Model, it allows models to maintain a 'dirty' state while still being able to fire events and participate in relationships (using the gallery-model-relate extension) until the model is deleted from the persistence layer via the 'delete' sync method (model.destroy({'delete': true})); Once the model is deleted from the persistence layer, calling the destroyFinal() method will prevent the model from being revived.
Note: In order to be able to destroy and revive objects multiple times, the destroy event is modified to no longer be a 'fireOnce' event. If you are listening to the destroy event via on or after, the event handler may be executed more than once. If your object does cleanup on the destroy event, it is recommended you use a once or onceAfter event or move your cleanup logic into a destructor if possible.
Example of using BaseRevive with Y.Model
<script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script>YUI({
//Last Gallery Build of this module
gallery: 'gallery-2011.11.17-14-56'
}).use('model', 'gallery-base-revive', function(Y) {
var SampleModel = Y.Base.create('sampleModel', Y.Model, [Y.BaseRevive], {}, {
ATTRS: {
firstName: {},
lastName: {}
}
});
var m = new SampleModel({
firstName: 'Joe',
lastName: 'Foo'
});
m.after('change', function(e) {
Y.each(e.newVal, function(val, atrr) {
Y.log(attr + ' changed to ' + val);
});
});
m.after('revive', function(e) {
Y.log('I am ALIVE!!!');
});
m.destroy();
m.get('destroyed'); // true
m.set('lastName', 'Zombie'); // logs 'lastName changed to Zombie' in console
m.revive(); // logs 'I am ALIVE!!!' in console
m.get('destroyed'); // false
m.destroyFinal(); // destroys the object and executes full destructor logic.
m.get('destroyed'); // true
m.revive(); // nothing happens since we called destroyFinal
m.get('destroyed'); // true
});
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info