【问题标题】:Trigger onload event in Backbone js在 Backbone js 中触发 onload 事件
【发布时间】:2013-06-21 08:21:07
【问题描述】:

好的,我有一个在 jQuery 中触发文档就绪事件的脚本,如何在主干中做同样的事情?那么我应该在哪里放置我的脚本:路由器、视图或模型?

这是我的标题视图

window.HeaderView = Backbone.View.extend({
 initialize: function (options) {
    this.render();
 },
 events : {
    "click .filter_button" : "filter_navigation",
    "click .search_button" : "live_filter_button",
    "keyup #live_filter" : "search"
},
 filter_navigation : function(e)
{       
       e.preventDefault();

    $('.filter').toggleClass('active');
    $('.search').removeClass('active');
},
live_filter_button : function(e)
{       
    e.preventDefault();

    $('.search').toggleClass('active');
    $('.filter').removeClass('active');
},
search : function(e)
{       
    var searchText = $("#live_filter").val().toLowerCase();
        $allListElements = $('.project_element');
        $matchingListElements = $allListElements.filter(function(i, el){
        return $(el).text().toLowerCase().indexOf(searchText) !== -1;
     });
    $allListElements.hide();
    $matchingListElements.show();
},
render: function () {
    $(this.el).html(this.template());
    return this;
}
});

【问题讨论】:

    标签: function backbone.js load ready


    【解决方案1】:

    您可以在render 事件中执行脚本代码。您可以将脚本包含在 html 文件本身中...... 例如:

    var Bookmark = Backbone.View.extend({
        template: _.template(…),
        render: function() {
            this.$el.html(this.template(this.model.attributes));
            return this;
        }
    });
    

    【讨论】:

    • 是的,但是我在哪里调用 OnRender 事件,你有例子吗?
    • 我的错; OnRender 在 Marionette 而不是 Backbone。我已经用 Backbonejs.org 上给出的示例更新了我的答案
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多