Skip to content

Instantly share code, notes, and snippets.

@mrDarcyMurphy
Created April 16, 2015 20:01
Show Gist options
  • Select an option

  • Save mrDarcyMurphy/5eefcf908ba3d26b02b2 to your computer and use it in GitHub Desktop.

Select an option

Save mrDarcyMurphy/5eefcf908ba3d26b02b2 to your computer and use it in GitHub Desktop.
Recursive Ampersand Model Clearing

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.

"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