2014-02-24 35 views
1

我想要用d3顯示一些存儲在MongoDB中的數據。我的問題是關於通過JADE模板爲每個數據創建div元素以及之後調用繪製不同圖表的方法的最佳實踐。
我的主要問題是在顯示HTML文件後我失去了對數據的引用,並且我不想再次查詢數據庫。在expressjs和JADE環境中對數據進行可視化

模式

# Create Schema 
executionSchema = new Schema(
    timestamp: Number, 
    components: [{ 
    uid: String, 
    type: { type: String }, 
    samples: [Number], 
    execution_times: [Number] 
    }] 
) 

的數據最初檢索並給予JADE模板:

指數咖啡

exports.index = (req, res) -> 
    Execution.find (err, executions, count) -> 
     res.render "index", title: "Debugger", executions: executions 
     return 
    return 

Afterwads的index.JADE用於創建divs每執行一次component [0]

- each component in executions[0].components 
    div(class="panel panel-primary") 
     div(class="panel-heading") UID: #{component.uid} 
     div(class="panel-body") 
     p(style='white-space:pre;') 
      | Type: #{component.type} 
     - var uid = component.uid 
     div(id=uid) 

這就是現在的一切,因爲我無法在JADE文件之外調用JavaScript方法。有任何想法嗎?
謝謝。

回答

1

如果我理解正確,你希望這個數據在客戶端腳本上可用,而不需要第二次實際查詢數據庫?

您有兩種選擇。第一個是剛剛加入這行權婁當前的玉代碼:

- each component in executions[0].components 
    // div creation stuff here 
    // ... 

script 
    window.executions = JSON.stringify(executions); 

現在您的客戶端腳本就能夠訪問處決對象和訪問,像這樣的數據:

var data = JSON.parse(executions); 

不知道這是否有效。如果您不想第二次查詢數據庫,可能是因爲數據集很大或數據庫速度較慢?

那麼只有一個數據庫查詢來做到這一點的另一種方式是呈現沒有div的頁面,並且根本不查詢數據庫。然後使用Javascript來獲取執行(ajax調用),並在數據加載後將其呈現在客戶端。

這取決於你的用例。

+0

謝謝你的回答。現在我正在構建div,並通過ajax調用添加數據。 – caiuspb

+0

這就是我將如何做到這一點。一次渲染視圖等視圖。在另一個進程中獲取數據。除非你正在尋找超級超級快速,那麼只需渲染服務器上的所有內容,並且不使用任何javascript :) – Zlatko