Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default (complex) attributes are by reference for multiple instances of a model. #3619

Closed
slikrik98 opened this issue May 20, 2015 · 3 comments
Labels

Comments

@slikrik98
Copy link

If you have an object specified as a 'default' attribute in your model, all instances of this model will share a reference to that object. Is there a reason we don't do a deep clone on the defaults object when creating a new Model instance?

As it is currently, if you make changes to a deep attribute of that object, you will be updating all model instances. Is this by design?

@jridgewell
Copy link
Collaborator

That's a common gotcha in all JavasScript, not just Backbone. You're better off using a function that returns your defaults instead.

var Model = Backbone.Model.extend({
  defaults: function() {
    return {
      deep: { object: true }
    };
  }
});

@slikrik98
Copy link
Author

Does Backbone discourage the use of objects for model attributes?

@Florian-R
Copy link

Yap, see #3457

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants