Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove an attribute from a Backbone.js model

Is there a way to remove an attribute from a Backbone model?

Reason being is I pass up extra data on save to perform certain actions, but then that data gets automatically added to my model

The documentation says to not edit the model.attributes directly, so the only other method I see to do this would be to use the set method and set the attribute to null, but that is not ideal

var myModel = new Model() myModel.save({name:'Holla', specialAttr:'Please Remove me'}) myModel.set({tempAttr:null})  if(myModel.attributes['specialAttr'] == null){     alert("Model does not have a specialAttr") } 

I've also tried removing it from the attributes property, but it doesn't really remove it.

like image 740
MattoTodd Avatar asked May 15 '12 17:05

MattoTodd


People also ask

How do I remove a model from a collection backbone?

js collection remove is used to remove a model or an array of models from the collection. Syntax: collection. remove(models,options)

How can we get the attribute value of a model in Backbone JS?

js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)

What is backbone JS model?

Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1. It extends Backbone.


1 Answers

Are you looking for model.unset ?

Remove an attribute by deleting it from the internal attributes hash. Fires a "change" event unless silent is passed as an option.

You can find the documentation here.

like image 169
Vincent Briglia Avatar answered Sep 25 '22 04:09

Vincent Briglia