我开发了第一个主干RESTfull应用程序。我有以下观点:
var app = app || {};
app.AssessorsCollectionView = Backbone.View.extend({
el: '#assessors',
tagName: 'table',
events:{
"submit form": "searchAssessors"
},
initialize: function() {
this.collection = new app.AssessorsCollection();
this.collection.fetch({reset: true, data: $.param({ online: false}) });
this.render();
this.listenTo( this.collection, 'reset', this.render );
},
render: function() { /// },
renderRegion: function( item ) { /// },
searchAssessors: function( e ) {
e.preventDefault();
this.collection.remove();
console.log("EVENT");
}
});
这是我的HTML表单:
<form id="form-search-assessors" class="form-horizontal" role="form">
.........
<input id="search-assessors-button" class="btn btn-lg btn-success btn-block" type="submit" value="Search" />
<!--<button id="search-assessors-button" class="btn btn-lg btn-success btn-block" type="submit">Search</button>-->
</form>
我给searchAssessors设置了断点,但它不起作用。此外,我在控制台中没有看到任何消息。
尝试将事件更改为"submit #form-search-assessors": "searchAssessors"
。
您初始化视图了吗?,由于这只是您代码中的一个片段,我删除了一些内容和注释,所以这应该可以工作。
感谢
HTML
<div id="assessors">
<form id="form-search-assessors" class="form-horizontal" role="form">
<input id="search-assessors-button" class="btn btn-lg btn-success btn-block" type="submit" value="Search" />
</form>
<div>
JS-
AssessorsCollectionView = Backbone.View.extend({
el: '#assessors',
tagName: 'div',
events:{
'submit form': 'searchAssessors'
},
initialize: function() {
this.render();
},
render: function() { },
searchAssessors: function( e ) {
e.preventDefault();
console.log('EVENT');
}
});
view = new AssessorsCollectionView();
我的问题的解决方案:
$( "form#form-search-assessors" ).submit($.proxy(function(e) {
e.preventDefault();
console.log(this);
this.collection.fetch({reset: true, data: $.param({ online: true}) });
this.render();
}, this));
问题是:https://stackoverflow.com/a/9813760/1756750