Backbone.js Simple way to clean Model

While working on a Backbone.js web app had to solve a small problem. Once you add some custom data to the model like this.model.fetch{data:{action: options}} attribute data will be added to the model. To get the model to it’s defaults you have two options will need to do .

First Option

this.model.unset(data); //where data is the key

Second Options would be to write a model function to do it.

clean:function(){
	for(a in this.attributes){
		if(!(a in this.defaults)){
			this.unset(a);
		}
	}
}},

With this option I don’t have to remember what customĀ attributes where added to the model.

 

Hope that helps!

Leave a Reply

Your email address will not be published. Required fields are marked *