I needed a way to clear out a model and all of it's children, and its children's children. I simply extended ampersand-model and overrode its clear method to do so. Now I just create all my models from it to ensure they get cleared all the way down.
Created
April 16, 2015 20:01
-
-
Save mrDarcyMurphy/5eefcf908ba3d26b02b2 to your computer and use it in GitHub Desktop.
Recursive Ampersand Model Clearing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| var _ = require("underscore"); | |
| var AmpersandModel = require("ampersand-model"); | |
| var BaseModel = AmpersandModel.extend({ | |
| clear: function () { | |
| // clear the model's attributes | |
| AmpersandModel.prototype.clear.call(this); | |
| // Go through the Model's children and collections to clear them too. | |
| _.forEach(this._children, function (constructor, child) { | |
| this[child].clear(); | |
| }, this); | |
| _.forEach(this._collections, function (constructor, collection) { | |
| this[collection].reset(); | |
| }, this); | |
| return this; | |
| } | |
| }); | |
| module.exports = BaseModel; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment