我已经定义了一个模型使用主干:
window.ResourceModel = Backbone.Model.extend({
default:{
'relativeurl':'unknow',
'type': "unkonw"
},
initialize: function(){
this.bind("change:relativeurl", function () {
console.log('change!',this.relativeurl);
});
this.bind("change:type", function () {
});
},
setRelativeURL: function (url) {
console.log('pass in',url);//this have value
this.set({"relativeurl": url});//this's value is undefined!
},
delResource: function () {
console.log("this.url",this.relativeurl);
resourceMasterView.delResourceItem(this.url);
}
});
然后我想调用这个方法
window.resourceModel = new ResourceModel();
resourceModel.setRelativeURL(url);
resourceModel.setType(type);
但只是我在上面评论,即使我已经调用了 set 方法,“relativeurl”结果仍然是未定义的!
我的代码有什么问题?我该如何解决这个问题?