0

在 Backbone 文档中,它说:如果您在属性哈希中设置 id,它将作为直接属性复制到模型中

但它不适用于我的代码:

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

这意味着我无法从我的收藏中获得这个模型:

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

以下是 Room 模型的一些代码片段:

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)});
        }

    });

我什至可以在一个简单的节点命令(版本 0.9.2,下划线 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 } }
>

所以我怀疑我的节点环境可能有问题?

4

0 回答 0