Search code examples
javascriptmongodbbackbone.js

Backbone unset only removes attribute locally


Is there a way to remove an attribute from a Backbone model both locally and in the database? I've tried doing something like this, but it only removes the attribute locally. I'm using MongoDB.

    model.unset(attribute);
    model.save();

Solution

  • Looks like I wasn't implementing the backend correctly. When you do

    model.unset(attribute).save();
    

    the object that is sent to the server by "save" contains all the attributes that the model contained before the call to "unset", but doesn't have the attribute removed by "unset". My problem was that I am using Mongo, and I when you use the Mongo "update" function, eliminating a key does not remove it from the database. You specify a "set" object, and to remove a key on update you have to specify a "unset" object, which I wasn't doing. More details here.