user240993
user240993

Reputation:

Backbone: grab new model from server before adding to collection

Currently when I make a model it goes straight to the collection and saves to the server, but the server adds additional information model that isn't seen until the page is refreshed. I'm trying to add the new model to the collection from the server and not from the form that makes the model.

This is my add method

add:function(tenant){
    var values = _.extend(this.$el.find(':input').serializeJSON(), {active: true , modelType:"tenant"}) 

    console.log(values)
    var newView = tenants.create(values, {// FIX REPONCE 
        success:function(model,response){
            console.log(response);
            console.log(model.isNew());
        },
        error:function(model,response){
            console.log(response.responseText);
        }
    },{wait: true},{silent: true})
}

When it hits the model.IsNew(), it returns true which means it didn't hit the server yet. How can I return the server model?

Upvotes: 0

Views: 133

Answers (1)

Hui Zheng
Hui Zheng

Reputation: 10224

The collection.create's second argument is options, but you passed options {wait: true} and {silent: true} as the third and fourth arguments respectively. That's why they take no effect. Try this:

var newView = tenants.create(values, {
    wait: true,
    silent: true,
    success:function(model,response){
        console.log(response);
        console.log(model.isNew());
    },
    error:function(model,response){
        console.log(response.responseText);
    }
});

Upvotes: 2

Related Questions