用户属性没有清除?

时间:2011-10-01 00:53:27

标签: javascript backbone.js coffeescript

我的用户模型遇到了一些问题,所以我添加了它:

logout: () ->
    console.log @toJSON()
    @clear()
    console.log @toJSON()
    if @id?
        alert @id

1st:console.log @toJSON()

Object
id: "4e862dc6e69aad002"
#other attributes
__proto__: Object

第二名:console.log @toJSON()

Object
__proto__: Object

但它仍会提醒身份证明......为什么会这样?

1 个答案:

答案 0 :(得分:1)

所以我没有骨干模型的经验,但这里有什么 发生

http://jsfiddle.net/

var Model = Backbone.Model.extend({
});

var x = new Model();

x.set({ "id": "hello world" });


alert(x.get("id"));
alert(x.id);
x.clear();
alert(x.id);
alert(x.get("id"));

只有最后一个返回undefined 所以我认为.get是它打算你使用的api女巫。

我读了骨干的源代码,它的ID是特殊的接缝,那是唯一的“字段” 它实际上直接放在对象上..而clear方法不会从中删除旧值。

这种接缝就像一个设计缺陷......但是id不应该改变任何方式????

我希望这有助于Boom

相关问题