我很难弄清楚为什么这不起作用。我在 Backbone 中有两个模型,我想通过移动到事件设置来分离它们。向您展示更容易:
var Session = Backbone.Model.extend({
url: '/some/url/session.js',
initialize: function(credentials) {
if (this.isValid() == true) {
this.trigger("start");
}
},
validate: function() {
if (this.get("user") != "user" || this.get("password") != "pass") {
this.trigger("end");
return "Invalid credentials.";
}
}
});
var MainModel = Backbone.Model.extend({
url: '/some/url/config.js',
initialize: function(credentials) {
this.session = new Session(credentials);
this.session.on("start", this.start());
this.session.on("end", this.end());
},
end: function() {
console.debug("session ended");
},
start: function() {
console.debug("session started");
}
});
new MainModel({
"user":"user",
"password": "pass"
});
我看到的问题是 MainModel.start() 和 MainModel.end() 在实例化后一直被触发。我不知道我是否正确添加了听众。
有任何想法吗?
谢谢!