Search code examples
javascriptjquerybackbone.jsunderscore.js

How to detect if the backbone.js model is cleared


js I wonder on how to detect if the model is cleared or empty?

if I set the model

model.set({name:'this is a test', id:1});

and clear it

model.clear();

Solution

  • You can test for this by doing the following:

    if ($.isEmptyObject(model.attributes)) {
        // model is cleared or empty
    }
    

    Note that you cannot rely on the model.isNew() function as it does not verify whether the model has had all of its attributes removed.

    You can see a working fiddle here.