我正在创建一个模型来获取用户配置文件数据然后使用它,但问题是每次获取数据(成功响应文本JSON可用)时模型都不会更新。所以我必须手动设置它。这是我的代码:
UserTest = Backbone.Model.extend({
defaults: {
userid: null,
full_name: null,
photo: null,
sec_access: null
},
urlRoot: 'data/userprofile.php',
parse: function (response) {
if (response.photo == null || response.photo == '') response.photo ='images/default-usericon.png';
}
});
var usertest = new UserTest;
usertest.fetch().then(function(user){
usertest.set('userid',user.userid);
usertest.set('full_name',user.full_name);
usertest.set('photo',user.photo);
usertest.set('sec_access',user.sec_access);
console.log(usertest.get('userid'));
});
有更好的方法吗?或者我做错了吗?
答案 0 :(得分:1)
您需要从parse
:
parse: function (response) {
if (response.photo == null || response.photo == ''){
response.photo ='images/default-usericon.png';
}
return response; // This is very important
}