ana
ana

Reputation: 653

Unable to make an update on the server with an id

I am updating a form and i want to make an update request on the serverwith an id my model is:

    var CampaignEditModel = Backbone.Model.extend({
    urlRoot:"http://localhost:3033/campaign/update/",
    url : function(){
       var url = this.urlRoot + this.id;
        return url;
    },
    idAttribute: "_id",
    defaults:{
        "id":null ,
        "Name" :""            
    }
});

render function is called here:

  $contents.empty().append(new EditView({model:editCampaigns}).render({id:id}).el);

and render function is:

render: function(options){
        this.$el.append( _.template(EditTemplate));
        this.model.set({"id":options.id})
        console.log(this.model.get("id"));
        this._modelBinder.bind(this.model, this.el);
        return this;
    },
    events: {
        'click .saveCampaign ': 'save'

    },
    save:function(){
        this.model.set({
            "Name" :$('#edname').val(),
        });
        this.model.save(null, {success: function(data){
            console.log("data:" + data);
            require(['campaignroute'],function(routes){
                var router = routes.pageRouter;
                router.navigate('gridView', {trigger: true});
            });
        }});
        return false;
    }

the problem is even i have set an id in the model still when save method is called the request go like this

http://localhost:3033/campaign/update/undefined

and console shows the eror:

Failed to load resource: the server responded with a status of 404 (Not Found)

how to solve this problem?

Upvotes: 0

Views: 203

Answers (2)

Xerri
Xerri

Reputation: 5046

Double check that the request is a post request and not a put request. 'Failed to load resource' errors is usually related to a missing request handler.

Upvotes: 1

Akos K
Akos K

Reputation: 7133

Instead of passing options to your custom render(options) function and setting the model id there, set the it directly on the editCampaigns model, before entering render(options):

editCampaigns.set('id', id);
$contents.empty().append(new EditView({model:editCampaigns}).render().el);

and remove the extra

this.model.set({"id":options.id})

from render(options) together with the options parameter. It should look like similar to this:

render: function(){
    this.$el.append( _.template(EditTemplate));
    console.log(this.model.get("id"));
    this._modelBinder.bind(this.model, this.el);
    return this;
}

Your model also has an extra url function:

url : function(){
   var url = this.urlRoot + this.id;
    return url;
}

you don't need this one since the models' id is automatically appended after urlRoot.


Unrelated to you problem I see you used

http://localhost:3033/campaign/update

to define your update URL. The HTTP method you use, already says what kind of action will be executed, this is the reason why you can (and should) write URLs without verbs. Just remove the extra /update.

Here is a quick summary about best-practices: How to create REST URLs without verbs?

Upvotes: 1

Related Questions