0

In Backbone document, it says that: If you set the id in the attributes hash, it will be copied onto the model as a direct property.

But it does not work on my code:

var room = new Room({id: 1});
test.equals(room.id, 1);// expect room.id to be 1, but it's undefined.

That means I cannot get this model from my collection:

rooms.add(room);
test.equals(rooms.get(1), room); // expect equal, but not because rooms.get(1) returns undefined

Here are some code snippets for the Room model:

var Room = Backbone.Model.extend({
        defaults: {
            dealInterval: 1
        },                 
        initialize: function(){   
            var seats = Seats.prepareSeats();   
            this.set({seats: seats, roomState: RoomState.WAITING, cards: Cards.decks(2)});
        }

    });

I could even reproduce this issue in a simple node command(version 0.9.2, underscore 1.3.3), :

username$ node
> var Backbone = require('backbone');
undefined
> var room = new Backbone.Model({id: 1});
undefined
> room.id
undefined // expect to be 1
> room
{ attributes: { id: 1 },
  _escapedAttributes: {},
  cid: 'c0',
  changed: {},
  _silent: {},
  _pending: {},
  _previousAttributes: { id: 1 } }
>

So I suspect there might be something wrong with my node env?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
aqingsao
  • 2,104
  • 1
  • 19
  • 18

0 Answers0