所以,我正在ASP.NET MVC页面中编写一个骨干应用程序,而且我对使用两个自定义事件感到非常困惑(对我而言)。
因此,MVC View(Planning.cshtml)有一个选择控件,触发Backbone路由器在更改时获取模型。这个模型加载需要大约10秒左右,所以我想在加载时禁用select以防止多个请求。为此,我让我的路由器触发两个自定义事件:'subview-loading'
和'subview-loading-complete'
,并让我的MVC视图监听这些事件。
我遇到的问题是路由器可以听到两个事件,但Planning.cshtml只会听到'subview-loading-complete'
事件,而不是第一个事件。
Planning.cshtml代码:
$(document).ready(function () {
var router = new Router({
el: '#TermPlanningReport'
});
var $routerEl = $(router.el);
$routerEl.bind('subview-loading', function() {
console.log('Planning.cshtml heard subview-loading');
//disable control
});
$routerEl.bind('subview-loading-complete', function() {
console.log('Planning.cshtml heard subview-loading-complete');
//enable control
});
});
Router.viewTerm() - 加载模型的路由方法:
viewTerm: function (term) {
var that = this,
termModel = new PlanningTerm({ term: term }),
$thisEl = $(that.el);
//for testing purposes, see if router can hear itself...
$thisEl.on('subview-loading', function() {
console.log('router heard subview-loading');
});
$thisEl.on('subview-loading-complete', function () {
console.log('router heard subview-loading-complete');
});
//trigger loading
$thisEl.trigger('subview-loading');
// fetch data for model
termModel.fetch({
success: function (model, response, options) {
$thisEl.trigger('subview-loading-complete');
//render a vew...
},
error: function (model, response, options) {
$thisEl.trigger('subview-loading-complete');
//render a view...
}
});
},
最后,我的控制台输出:
router heard subview-loading
router heard subview-loading-complete
Planning.cshtml heard subview-loading-complete
变量$routerEl
和$thisEl
都正确地返回相同的元素。