silkAdmin
silkAdmin

Reputation: 4810

How to check if a backbone model exists on the server

If i do

var model = new Model({ id : 'someNewId'});
model.fecth();

I then get a model filed with default value as the model wiht an idea of 'someNewId' does not exist on server.

What would be the best way to ensure that a model does exist on the server?

Upvotes: 3

Views: 1276

Answers (1)

jevakallio
jevakallio

Reputation: 35920

It really depends on your server configuration. Typically a RESTful service would return a HTTP error code 404 to indicate that the resource was not found.

model.fetch({
    success: function(model, response, options) {
        //model exists and is now populated
    },

    error: function(model, xhr, options) {
        if(xhr.status === 404) {
            //model was not found
        } 
        else {
            //some other error
        }
    }
}

Upvotes: 6

Related Questions