2013-05-12 36 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