2013-05-12 80 views
0

由于上面的骨干事件,我无法检测到方向。它不起作用。除此之外,还有其他方法吗?通过绑定一个事件或其他东西?或者我的代码中有什么错误?事件orientationchange不起作用在主干

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

指http://stackoverflow.com/questions/1649086/detect-rotation-of-android-phone-in-the-browser- with-javascript – 2013-05-12 19:24:58

+0

'onorientationchange'是窗口对象的属性。现在,您正试图将其应用于Backbone View。 – zvona 2013-05-12 19:28:39

回答

2

如果你设置视图elwindow它的工作只是以防万一。

也可以手动订阅orientationchange事件:

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

嗯我很抱歉,但什么是埃尔,我怎么能把它设置为窗口? – 2013-05-12 19:30:45

+0

你可以在库文档中阅读关于'el'的更多信息http://backbonejs.org/#View-el – 2013-05-12 19:32:38

+0

如果我设置el:$(窗口),可以吗? – 2013-05-12 19:56:15