2011-10-19 52 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