[英]Property change value beetwen console.log in Backbone Model
I have simple user model: 我有简单的用户模型:
var user = Backbone.Model.extend({
initialize: function(){
this.bind("change:auth", function (){
if(this.get("auth") == true){
$.cookie('auth', true);
$.cookie('username', this.get("username"));
$.cookie('sessid', this.get("sessid"));
console.log(this)
console.log(this.attributes)
}else{
$.cookie('auth', null);
$.cookie('username', null);
$.cookie('sessid', null);
}
});
},
defaults : {
'auth' : $.cookie('auth'),
'username' : $.cookie('username'),
'sessid' : $.cookie('PHPSESSID')
},
logout : function (){
this.clear();
}
});
I don't know why it's happening, but when using the set
method (and firing change event) the first console log is: 我不知道为什么会这样,但是当使用
set
方法(以及触发change事件)时,第一个控制台日志是:
_callbacks
Object { change:auth=[1]}
_changed
false
_changing
false
_escapedAttributes
Object {}
_previousAttributes
Object { auth=true, username="admin", sessid="roo74p7m7t0a3erke8r3vsat33"}
_unsetAttributes
null
attributes
Object { auth=true, username="admin", sessid="roo74p7m7t0a3erke8r3vsat33"} //<-- under this.attributes.username is string
cid
"c0"
defaults
Object { sessid="roo74p7m7t0a3erke8r3vsat33", auth=null, username=null}
idAttribute
"id"
constructor
function()
_performValidation
function()
bind
function()
change
function()
changedAttributes
function()
clear
function()
clone
function()
destroy
function()
escape
function()
fetch
function()
get
function()
has
function()
hasChanged
function()
initialize
function()
isNew
function()
logout
function()
parse
function()
previous
function()
previousAttributes
function()
save
function()
set
function()
toJSON
function()
trigger
function()
unbind
function()
unset
function()
url
function()
__proto__
Object { defaults={...}, _changed=false, idAttribute="id"}
Everythings seems to be ok, but then the second console.log (this.attributes) is: 一切似乎都还可以,但是第二个console.log(this.attributes)是:
Object { auth=true, sessid="roo74p7m7t0a3erke8r3vsat33", username=null} // oh no now it's null
In conclusion, value of this.attributes.username was changed whitout any action on it. 总之,在不执行任何操作的情况下,this.attributes.username的值已更改。 Any idea how I can resolve it?
知道我该如何解决吗?
This is just a little quirk related to Firebug. 这只是与Firebug相关的一个小怪癖。 I assume you're setting a few properties at one time, like this:
我假设您一次要设置一些属性,如下所示:
var myuser = new user();
myuser.set({sessid:"roo74p7m7t0a3erke8r3vsat33", auth:true, username:"admin"});
What's happening is this, the change:auth
event is being called before the username has been set in the model. 发生的事情是,在模型中设置了用户名之前 ,将调用
change:auth
事件。 Because of this the second console.log
is correct, no username has been set yet, so it doesn't show up there. 因此,第二个
console.log
是正确的,尚未设置用户名,因此该用户名不会在那里显示。
The reason you're seeing everything correctly in the first console.log
is because you're logging a reference to the user
object, not a copy . 您在第一个
console.log
正确看到所有内容的原因是因为您正在记录对user
对象的引用, 而不是copy 。 By the time you click on the object in your console, all of the attributes have since been set, so you see them all there. 在您单击控制台中的对象时,所有属性均已设置完毕,因此您可以在此处看到所有属性。
So your code is functioning correctly, all properties are being set. 因此,您的代码正常运行,所有属性都已设置。 You should be able to verify this by adding a
console.log
right after you set the properties, like so: 设置属性后,您应该能够通过添加
console.log
来验证这一点,如下所示:
var myuser = new user();
myuser.set({sessid:"roo74p7m7t0a3erke8r3vsat33", auth:true, username:"admin"});
console.log(myuser.attributes);
Another way to prove the case, if you move username:"admin"
to before auth:true
in your the set
object, like this: 另一种方式来证明的情况下,如果移动
username:"admin"
前auth:true
在你的set
对象,像这样:
var myuser = new user();
myuser.set({sessid:"roo74p7m7t0a3erke8r3vsat33", username:"admin", auth:true});
You should see the username show up in the second console.log
since the username attribute will have been set before change:auth
is called. 您应该看到用户名显示在第二个
console.log
因为将在调用change:auth
之前设置用户名属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.