我已经创建了在主干视图上触发两次警报的非常快速的示例。
http://jsfiddle.net/feronovak/8svqX/
这没什么特别的,它真的是让我了解如何正确调用单独的视图,所以它们不会触发以前的点击方法。现在我生成了两个视图,第二个视图覆盖了#boxSearch 中的表单。当我单击按钮 Search 时,它也会生成 alert doSearch SearchLocation
。我期待只看到 alert doSearch SearchLatitude
。我做错了什么?
var Geo = {
Views: {},
Templates: {}
};
Geo.Templates.SearchLocation = _.template( $("#tpl-search-location").html());
Geo.Templates.SearchLatitude = _.template( $("#tpl-search-latitude").html());
Geo.Views.SearchLocation = Backbone.View.extend({
initialize: function() {
this.render();
},
el: $("#boxSearch"),
template: Geo.Templates.SearchLocation,
render: function()
{
$(this.el).html(this.template);
},
events: {
"click input[type=button]": "doSearch"
},
doSearch: function(e) {
e.preventDefault();
alert('doSearch SearchLocation');
}
});
Geo.Views.SearchLatitude = Backbone.View.extend({
initialize: function() {
this.render();
},
el: $("#boxSearch"),
template: Geo.Templates.SearchLatitude,
render: function()
{
$(this.el).html(this.template);
},
events: {
"click input[type=button]": "doSearch"
},
doSearch: function(e) {
e.preventDefault();
alert('doSearch SearchLatitude');
}
});
$(document).ready(function(e) {
var searchLocation = new Geo.Views.SearchLocation();
var searchLatitude = new Geo.Views.SearchLatitude();
});