事件的方向改变不能在主干中工作

The events orientationchange don't work in backbone

本文关键字:工作 不能 方向 改变 事件      更新时间:2023-09-26

上面的事件在主干中,我无法检测方向。这行不通。还有别的选择吗?通过绑定事件或其他方式?还是我的代码有问题?

var HomeView = Backbone.View.extend({
    template: Handlebars.compile(template),
    events: {
        "click .log_out": "log_out",
        "click .prove": "prove",
        "orientationchange": "onOrientationChange"
    },
    initialize: function () {
        console.log("inhomeview");
        this.render();
    },
    render: function () {
        //var context = JSON.parse(JSON.stringify(this.model));//eliminare
        var context = this.model;
        var html = this.template(context);
        console.log(html);
        $('#pagina').empty();
        $('#pagina').append(this.$el.html(html));
        return this;
    },
    onOrientationChange: function () {
        if (window.orientation === 90 || window.orientation === -90) {
            alert('you are in landscape');
        }
    }
});

只有在将视图el设置为window的情况下才能工作。

或者您可以手动订阅orientationchange事件:

initialize : function() {
    $(window).on('orientationchange', this.onOrientationChange);
}