Reputation: 2754
I'm creating new entity using breeze in this way:
var item = manager.createEntity("ExercisesAndMuscles", initialValues);
saveChanges().fail(addFailed);
logger.log("id is", item.Id);
function saveChanges() {
return manager.saveChanges();
}
And in console is -1, and according to Breeze documentation this is as it should be. And when I refresh page I see that id is not -1 anymore, i.e. now id is permanent id form database.
So my question is, how can I get this id automatically i.e. without refreshing page. I know that one possible solution would be to generate ids on client side, but I'd rather not do that.
Are there any other ways ? And if generating id on client side is only way, what would be proper way to do this ?
Upvotes: 2
Views: 455
Reputation: 14995
saveChanges occurs asynchronously, so at the time that you are logging the id is indeed -1 but some time shortly thereafter it becomes permanent.
var item = manager.createEntity("ExercisesAndMuscles", initialValues);
saveChanges().then(showId).fail(addFailed);
function showId () {
logger.log("id is", item.Id);
}
function saveChanges() {
return manager.saveChanges();
}
log the id after the returned promise is completed like shown and it will no longer be -1.
Upvotes: 1