事件方向改变在骨干中不起作用

时间:2013-05-12 19:17:26

标签: javascript backbone.js

由于骨干上面的事件,我无法检测到方向。它不起作用。有没有替代方案?通过绑定事件或其他东西?或者我的代码中有什么问题?

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');
        }
    }
});

1 个答案:

答案 0 :(得分:2)

仅当您将视图el设置为window时才会有效。

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

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