2011-10-19 47 views
0

目前,骨幹路由器的設置,使它逐漸通過每個模板(jQuery模板)並根據模型狀態同化視圖的各個部分。如何在DOM元素上設置事件觸發器,而骨幹視圖仍在裝配DOM元素?

在某些路由器的方法,我呼籲lhsview.render和rhsview.render

$(appView.el).append(this.lhsView.render().el); 
    $(appView.el).append(this.rhsView.render().el); 
    appView.render(); 

在rhsView,

initialize : function(){ 
     this.initializeGraph(); 
    }, 
    initializeGraph : function(){ 
     $('#product-graph').tmpl({this.options.graph}).appendTo($(this.el));   
     // Call plot from jquery flot 
     $.plot($("#graph"), data, options); 
    } 

問題與調用$ .plot()是與ID的DIV =圖形尚未附加到DOM。我如何確保我可以在id = graph的div上設置$ .plot()它在initializeGraph方法本身中的DOM內?

回答

2

如果#graph元素是你的看法的el的一部分,你可以通過你的觀點

RHSView = Backbone.View.extend({ 
    initializeGraph: function(){ 
    $('#product-graph').tmpl({this.options.graph}).appendTo($(this.el));   

    // if your template generated the #graph 
    // then it will be available from within this 
    // view's el 
    var graph = this.$("#graph"); 

    $.plot(graph, data, options); 
    } 
}); 

內調用從this.$("#graph")訪問這將正確選擇#graph,然後在其上繪製的一切。然後,將元素添加到DOM時,它將正確顯示。

我在將這些信息附加到DOM之前預先渲染內存中的信息。

+0

真棒Derick。完美工作。 – papdel