Jquery骨干查看侧面调用

时间:2013-06-05 02:12:02

标签: jquery backbone.js

但我尝试这不起作用,什么是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,但我在这里遇到了一些问题......请在下面提供一些帮助:

我在下面有一个BackView的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") });

1 个答案:

答案 0 :(得分: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方法