1

但我尝试这不起作用,directory.views.SearchPage 是什么?我可以像上面那样调用这个函数吗?谢谢你的分享!

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();
    //var thiskey = ' ';
    //this.model.findByName();
    //alert("HELLO WORLD!1");
    return this;
},
events: {
    "keyup .search-key": "search"
},
// Start the search function
search: function(event) {
    var key = $('.search-key').val();
    this.model.findByName(key);
}
});

我尝试学习 Jquery,但我在这里遇到了一些问题......请在下面提供一些帮助:

我在下面有一个 Backbone 的 SearchView,我希望页面加载运行这个 SearchView,我编写了一个事件页面“准备好”和“加载”,但是页面加载时没有触发......

我有一个问题如何使页面加载运行此功能?或者如何在外部函数中调用这个 SearchView 主干?

    SearchView = Backbone.View.extend({
    initialize: function(){
        this.render();
    },
    render: function(){
        var template = _.template( $("#search_template").html(), {} );
        this.$el.html( template );
    },
    events: {
        "click input[type=button]": "doSearch",
        "keyup": "doSearch",
        "load": "doSearch",
  "ready": "doSearch"  
    },
    doSearch: function( event ){
        // Button clicked, you can access the element that was clicked with event.currentTarget
        alert( "Search for " + $("#search_input").val() );
    }
});

var search_view = new SearchView({ el: $("#search_container") });
4

1 回答 1

1

您将希望在页面加载后实例化您的视图。

var search_view ;

// jQuery loads yoru view on document ready
$(function () {
    search_view = new SearchView({ el: $("#search_container") });
});

如果你想在外部调用 doSearch 函数,你只需要这样做:

search_view.doSearch();

假设您想在页面加载时执行 doSearch,您可能希望在this.render();调用后将其添加到 initialize 方法中

于 2013-06-05T03:17:59.280 回答