0

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?

ana
  • 653
  • 1
  • 9
  • 20
  • i did what you have said to me and now id is set but still i got this error: Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3033/campaign/update/5c72e198-c8fd-11e2-9cfa-74867a028324 @akoskm – ana May 30 '13 at 10:37

2 Answers2

1

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?

Community
  • 1
  • 1
Akos K
  • 7,071
  • 3
  • 33
  • 46
  • I did what you have said now the request looks like this http://localhost:3033/campaign/update/5c72e198-c8fd-11e2-9cfa-74867a028324 – ana May 30 '13 at 10:31
  • and still i get this error: Failed to load resource: the server responded with a status of 404 (Not Found) @akoskm – ana May 30 '13 at 10:32
  • /update is not problem bcz on server the post request is: app.post('campaign/update/:id', campaign.update); – ana May 30 '13 at 10:36
  • @ana It's okay, that's the value you have in `id`. Backbone tries to execute HTTP POST method if the resource doesn't exist (no id) and HTTP PUT if the the resource already exists (has id). You must have the appropriate REST Resource to handle these requests. – Akos K May 30 '13 at 10:37
  • 1
    @ana in this case you will also need a PUT resource. – Akos K May 30 '13 at 10:39
  • if i write like this: app.put('campaign/update/:id', campaign.update); then i got the error: OPTIONS http://localhost:3033/campaign/update/4b9cf0f8-c915-11e2-9cfa-74867a028324 200 (OK) jquery-1.7.2.min.js:4 PUT http://localhost:3033/campaign/update/4b9cf0f8-c915-11e2-9cfa-74867a028324 404 (Not Found) 4b9cf0f8-c915-11e2-9cfa-74867a028324:1 @akoskm – ana May 30 '13 at 10:43
  • @ana this has nothing to do with backbone. You forward the server the id of you model has, and this is OK. Try to call http://localhost:3033/campaign/update/4b9cf0f8-c915-11e2-9cfa-74867a028324 from your browser, it should either return the model for that id or return a method not allowed status (405) – Akos K May 30 '13 at 10:48
1

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.

Xerri
  • 4,916
  • 6
  • 45
  • 54