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

  • SEARCH
  • Home
  • 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 9074421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:37:55+00:00 2026-06-16T18:37:55+00:00

I have a User model(Backbone.js) and I want to update its settings attribute and

  • 0

I have a User model(Backbone.js) and I want to update its settings attribute and then save it to the server. Settings is in JSON format, and the way I have it set up is that settings is the string version and settingsJSON is the object version. I bind functions to the change event of each so that when one changes, it updates the other.

The problem I am having, is that the save method is running before the changed handler is finished running. Is there any way i could ensure that all event handlers for that model are complete or something like that?

how I’m calling it:

currentUser.get('settingsJSON').apps = appsEnabled;
currentUser.save();

My event handlers:

Initialize: function() {
    var that = this;
    this.on("change:settingsJSON", function(model){
        model.set({settings: JSON.stringify(model.get('settingsJSON'))});
    });

    this.on("change:settings", function(model){
        model.set({settingsJSON: JSON.parse(model.get('settings'))});
    });
}

@fencliff:

The change event is firing when I run this and works properly, I had it print the new settings string to the console.

Are you sure that they are called synchronously? I added console.log('changed') to the end of the .on(change) and put console.log('saved') directly after currentUser.save() and every time the console read:

saved
changed

For now I have just made it so that I stringily the JSON and save it to settings directly before I save and that works fine.

  • 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-16T18:37:56+00:00Added an answer on June 16, 2026 at 6:37 pm

    Backbone events are executed synchronously. That means that unless you (or some library) has overridden some part of the event handling, the change handlers will have processed as soon as you execute the next line of code.

    In you code example there is another problem. When you call

    user.get('settingsJSON').apps = appsEnabled;
    

    The change event will not fire, because the value of settingsJSON has not been changed, merely the contents of the object were modified. The model.attributes.settingsJSON is still the same object as before.

    The events are fired only when you call set on the property, and the new value is a different object. For example:

    user.set('settingsJSON', _.extend({}, user.get('settingsJSON'), {apps:appsEnabled});
    

    Another problem, it would seem, is that your event handlers, if triggered, would cause the change event being fired twice for the property which was first set:

    this.on("change:settingsJSON", function(model){
        //-> changes settings, and set triggers change
        model.set({settings: JSON.stringify(model.get('settingsJSON'))});
    });
    
    this.on("change:settings", function(model){
        //-> changes settingsJSON, and set triggers change
        model.set({settingsJSON: JSON.parse(model.get('settings'))});
    });
    

    To solve that issue, call set with {silent:true} or modify the model.attributes hash directly.

    Edited with corrections by @muistooshort.

    Edited again with further corrections

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

Sidebar

Related Questions

I have a Backbone application where the JSON I get from the server isn't
I have the following user model: var UserModel = Backbone.Model.extend({ urlRoot: 'user', defaults: {
I have a rails app using backbone, but when I call save, the server
In Backbone, I have a User model: var User = Backbone.Model.extend({ url: '/api/user' });
I have a Backbone model: var User = Backbone.Model.extend({ idAttribute: '_id', url: '/api/user', defaults:
I have an application in rails 3 using backbone.js When I use @model.save, i
I have the following model and collection: var UserModel = Backbone.Model.extend({ url: 'api/user', idAttribute:'username',
I have simple user model: var user = Backbone.Model.extend({ initialize: function(){ this.bind(change:auth, function (){
I currently have two Backbone models: user and project. I would like to have
Let's say I have User model and Post model. Post model contains field user_id

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.