How to mass assign all attributes of a Backbone model?

attributes, backbone.js, model

Solution

- You are moving around a `Backbone.Model`,

- `Model.set` accepts a hash of attributes,

- you can convert a `Backbone.Model` to a hash of attributes with `Model.toJSON`

You could write your callback as

success: function(model, response, options) {
    self.model.set(model.toJSON());
}

Problem

I would like to update the `User` model whenever `signIn` was successful. It includes the `id` assign by the backend which has not been present in the model before. ``` MyApp.module("User", function(User, App, Backbone, Marionette, $, _) { User.Controller = Backbone.Marionette.Controller.extend({ initialize: function() { this.model = new MyApp.User.Model(); }, signIn: function(credentials) { var signInData = { user: credentials }; var self = this; App.session.signIn(signInData, { success: function(model, response, options) { self.updateUserModel(model); }, error: function(model, xhr, options) {} }); }, updateUserModel: function(model) { // TODO Update all attributes, including new onces e.g. id. } }); }); ``` How would you update all attribtues at once? I know that I manually can `set` every single attribute but this seems to be wrong since the list of attributes may change over time. Generally, I would expect such an `update(model)` method in the `User` model. When I use Backbone's `model.set()` method as suggested by nikoshr and john-4d5 ... ``` signIn: function(credentials) { var signInData = { user: credentials }; var self = this; App.session.signIn(signInData, { success: function(model, response, options) { self.model.set(model); }, error: function(model, xhr, options) {} }); }, ``` ... the `id` attribute is copied into `this.model` but other properties such as `name` are missing. The model returned in the `success` callback looks like this: ``` _changing: false _pending: false _previousAttributes: Object attributes: Object bind: function (name, callback, context) { close: function (){ constructor: function (){ return parent.apply(this, arguments); } created_at: "2013-07-22T19:03:24Z" email: "user@example.com" id: 3 initialize: function () { listenTo: function (obj, name, callback) { listenToOnce: function (obj, name, callback) { logout: function () { model: child name: "Some User" off: function (name, callback, context) { on: function (name, callback, context) { once: function (name, callback, context) { options: Object signIn: function (credentials) { signUp: function (credentials) { stopListening: function (obj, name, callback) { trigger: function (name) { triggerMethod: function (event) { unbind: function (name, callback, context) { updated_at: "2013-08-05T13:20:43Z" user: Object __proto__: Object changed: Object cid: "c3" id: 3 __proto__: Surrogate ```

Original source