我是backbone.js的新手,我在使用带有ASP.NET MVC 4的backbone.js。
我有一个名为SomeObject的全局类,其中有一个deleteUser函数。此功能与MyView2中存在的按钮的单击事件之一绑定。
如何在此全局函数/类中调用以下backbone.js不同视图中存在的不同函数。
请指导我。我还在学习backbone.js,可能会做错事。感谢
var SomeObject = function (Id, Name) {
var self = this;
this.Id = Id;
this.Name = Name;
this.deleteUser = function () {
console.log(self.Id, self.Name);
// call myMethodB of MyView 2
// call myMethodA of MyView 1
// call myMethodC of AppView
};
};
var MyModel = Backbone.Model.extends({
});
// View for a Main Grid
var MyView1 = Backbone.View.extend({
...
myMethodA: function(){
// do something with View 1
}
...
});
// View for subgrid in Main Grid
var MyView2 = Backbone.View.extend({
...
myMethodB: function(){
// do something with View 2
}
...
});
var AppView = Backbone.View.extend({
...
myMethodC: function(){
// do something with App View
}
...
});
答案 0 :(得分:1)
您应该使用Backbone事件 - http://backbonejs.org/#Events
var SomeObject = function (Id, Name) {
var self = this;
this.Id = Id;
this.Name = Name;
this.deleteUser = function () {
console.log(self.Id, self.Name);
viewInstances.MyView2.trigger('eventFormyMethodB')
viewInstances.MyView1.trigger('eventFormyMethodA')
viewInstances.AppView.trigger('eventFormyMethodC')
};
};
var viewInstances = {
MyView2: new MyView2({...}),
MyView1: new MyView1({...}),
AppView: new AppView({...})
}
例如
var AppView = Backbone.View.extend({
...
events: {
'eventFormyMethodC': 'myMethodC'
},
myMethodC: function(){
// do something with App View
}
...
});