0

我尝试了事件加载或准备运行搜索:函数(事件),当页面加载时无法生效,但是keyon可以执行搜索功能。我可以在外面调用搜索功能吗?可以调用页面加载吗?请帮忙,我是 jquery 的初学者。

directory.views.SearchPage = Backbone.View.extend({

templateLoader: directory.utils.templateLoader,
EmployeeListView: directory.views.EmployeeListView,

initialize: function() {
  this.template = _.template(this.templateLoader.get('search-page'));
},

render: function(eventName) {
  $(this.el).html(this.template(this.model.toJSON()));
  this.listView = new directory.views.EmployeeListView({el: $('ul', this.el), model:this.model});
  this.listView.render();
  return this;
},
events: {
  "keyup .search-key": "search",
  "load .search-key": "search",
  "ready .search-key": "search"
},
// Start the search function
search: function(event) {
  var key = $('.search-key').val();
  this.model.findByName(key);
}
});
4

1 回答 1

0

events哈希仅侦听在您的视图上/在您的视图中发生的事件。

您需要手动订阅document.ready

initialize: function () {
    var self = this;
    this.template = _.template(this.templateLoader.get('search-page'));

    $(document).ready(function (event) {
        self.search(event);
    });
}
于 2013-06-05T15:19:33.967 回答