2011-10-01 46 views
0

我有一些麻烦我的用户模型,所以我说这是:用户属性没有清除?

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

1:执行console.log @toJSON()

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

第二:的console.log @toJSON()

Object 
__proto__: Object 

但它仍然提醒ID .. w^hy可以吗?

+0

@clear()在做什么? – dteoh

+0

我想要se toJSON metod plz – megakorre

+0

http://documentcloud.github.com/backbone/#Model-clear – boom

回答

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 ,所以我认为asume是。获得API的巫婆它打算你使用。

我读了主干的源代码,它接缝的ID是特殊的,这是唯一的“字段”,它实际上直接放在对象上..和清除方法DOS不会从这里删除旧值。

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

我希望这可以帮助Boom

+0

啊,我明白了,是的,你是对的。我必须重置属性外的硬集ID。如果用户有一个ID我的应用程序认为他们登录,所以这就是为什么我需要清除它时注销。谢谢! – boom

+0

正确,常见骨干错误:混淆属性与属性。 'clear'重置'@ attributes','toJSON'只是给你一个'@ attributes'的克隆;与'@ id'(与'@ attributes.id'相对)都没有任何关系。 –