Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:04:02+00:00 2026-06-17T05:04:02+00:00

i am new to backbone.js and i am trying to make a simple model

  • 0

i am new to backbone.js and i am trying to make a simple model work with a restful API but i cant seem to make the simple POST operation work.

from what i understand if a model element does not have the property “id” set and the save() function is called, the model will issue a post command to the server (the url of the model)
and will need to receive back an HttpStatus.OK (200) and the new id of the item.

here is the JS code:

  this.model = new MYAPP.menuItem();
  this.model.save();
  console.debug("the new id is:", this.model.get("id")) <-- this is undefined

i am using spring controller as the server side. this is the controllers code

@RequestMapping(value = "/menuItem",method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK )
@ResponseBody
public Long addmenuItem(Model model) {
    return new Long(1);
}

The Post call is reaching the server ok and the controller is called but as you can see the id of the model is not beeing updated.

what am i doing wrong?

thanks.

Additional code as requested:

    initialize: function() {
  console.debug("initializing new view");
  this.model = new MYAPP.Event();
  this.model.save({
      success: function( model ) {
        console.log( model.get('id') );
      }
    });
  var source   = $("#index-template").html();
  this.template = Handlebars.compile(source);
  console.debug("view collection", this.model);
  this.model.on('all', this.render, this);
  //this.recipes.fetch();
},

sorry for the mess!.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:04:03+00:00Added an answer on June 17, 2026 at 5:04 am

    .save() method is asynchronous. As so, the id isn’t returned by the server right away. Try it like this:

    var self = this;
    this.model.save().done(function() { console.log( self.model.get('id') ); });
    

    edit As you have trouble with reference to your object, you can have the same result this way around:

    this.model.save({
      success: function( model ) {
        console.log( model.get('id') );
      }
    });
    

    edit after some chat Seems like the returned data wasn’t valid. After a save, the server should return the full object encoding with json (with the id).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a quick sample Backbone.js app that has a Post model,
I am new trying to make use Routers in backbone but my Backbone.history.start() method
Trying to create a backbone plugin that inherits from Backbone.Model but overrides the sync
I'm very new to backbone but I manage to get it working from tutorial.
I'm trying to get some animation to work during a Backbone View's render operation
I am using backbone.js, and trying to fetch some json from twitter, but it
I am trying to implement a simple Backbone app on top of RequireJS, but
I'm kind of new to backbone coming from knockout.js and I'm trying to get
I'm trying to add a new virtual field to my backbone model whenever a
I'm new to backbone and trying to set it up in Sinatra, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.