admin管理员组

文章数量:1392002

I've inherited a backbone js based app. I really like backbone and i'm just starting to get my head around it. From my understanding when model.save is called on a new entity it should post that to the server, the server should return the same json but with an id alloted and backbone should persist that id to the model so that further saves result in a PUT with the ID for update.

However, when I call model.save() and then try to get the model.id property, it's null.

Is this because I'm not doing it with a call back? So the property hasn't been set yet?

How would I set the success callback? calling model.save({success: function(){...}})doesn't work?

here is the actual call:

model.save(null, {
    success: function () {
        alert('success');
    },
    error: function () {
        alert('error');
    }
});

I've inherited a backbone js based app. I really like backbone and i'm just starting to get my head around it. From my understanding when model.save is called on a new entity it should post that to the server, the server should return the same json but with an id alloted and backbone should persist that id to the model so that further saves result in a PUT with the ID for update.

However, when I call model.save() and then try to get the model.id property, it's null.

Is this because I'm not doing it with a call back? So the property hasn't been set yet?

How would I set the success callback? calling model.save({success: function(){...}})doesn't work?

here is the actual call:

model.save(null, {
    success: function () {
        alert('success');
    },
    error: function () {
        alert('error');
    }
});
Share Improve this question edited Sep 28, 2011 at 18:05 Jacob 78.9k24 gold badges157 silver badges241 bronze badges asked Sep 28, 2011 at 12:32 user156888user156888 6
  • Have you tried to set the error callback too? – mamoo Commented Sep 28, 2011 at 12:41
  • have you verified that the server does indeed get called? – timDunham Commented Sep 28, 2011 at 13:09
  • Did you create your 'model' using Backbone.Model.extend? – mamoo Commented Sep 28, 2011 at 13:10
  • is the idAttribute set? this is the way to change the default 'id' to be something else (like, say 'entityId') – timDunham Commented Sep 28, 2011 at 13:14
  • can you post the response that your server provides, including the json result and the http header status? – Derick Bailey Commented Sep 28, 2011 at 13:33
 |  Show 1 more ment

3 Answers 3

Reset to default 4

Something feels odd about this. Setting silent: true only makes it so none of the events get fired. Everything else should happen normally. In other words, don't assume that setting slient: true is the right answer here...

I suspect you are actually throwing an exception some place (probably with validation or something like that) and somehow, setting silent: true is causing everything to flow through.

I would strongly suggest that you remove this option and check your console or run with the debugger... I suspect you have a bug lurking around there some place.

Some suggestions: Take a look at the annotated source for the model.set function. It gets called before your success callback will get called. Inside of that function, there are several things that will happen if silent is false. These include validation, individual property change triggers, and a global change trigger. I would bet money that either the validation is failing or something that is listening to the changes is throwing an exception.

i needed to set the silent: true on the save:

    model.save(null, {
        silent: true,
        success: function () {
            alert('success');
        },
        error: function () {
            alert('error');
        }
    });

I had the same issue, turned out to be because my custom parse was failing

本文标签: javascriptbackbonejs modelsave doesn39t set the idStack Overflow