2012-03-31 29 views
0

我有以下情況。Rails 3如何使用遞歸循環(構建樹圖)更新頁面?

在這個模型中,我填充表,其中有一個樹狀結構。我有一個HAS_MANY圖模型:即具有由以下中填充的「內容」一欄的節點和節點模型:

class Graph < ActiveRecord::Base 

    def start_collecting 
    self.content_collection(self.nodes.first) 
    end 

    def content_collection(root) 
    root.children.all(:order => "idx DESC").each do |node| 
    #Here the function in Node model is called, that populates 'content' column in node's table. 
    content_array << node.content.to_s 
    self.children_content = content.array 
    self.save! 
    child.collect_content 
    if !node.children.blank? 
     self.content_collection(node) 
    end 
    end 
    end 

的問題是..我想有進度顯示在網頁上頁面(app/views/graphs/show.html.erb)。所以

setInterval(function(){ 
    var text = <%= array_or_string_for_javascript(@graph.children_content)%> ; 
    var pjs = Processing.getInstanceById("graphsketch"); 
    pjs.update(text); 
}, 3000); 

,我只是重繪@ graph.children_content在屏幕上的內容每3秒..我不知道什麼是制定每個@node的內容,最好的辦法:到目前爲止,我這樣做。這個定時刷新的內容?顯然,如果我只是寫一個。每個循環中show.html.erb我會不斷收到在循環的最後@node的內容......基本上,我不知道如何從上述模型中的循環轉換到視圖,使進度可見?

希望這是非常明顯的,請讓我知道我是否應該更好地解釋......

編輯::

所以下面的建議,現在我在graph_controller.rb:

def getstatus 
    @graph = Graph.find(params[:id]) 
    @graph_so_far = @graph.get_graph 
    respond_to do |format| 
    format_js 
    end 
end 

在Graph.rb:

def get_graph(root = self.nodes[0], word_ary = []) 
    @root = root 
    @root.children.all(:order => "idx DESC").each do |child| 
    n_node = (child.depth*2).to_s+child.content 
    word_ary << n_node 
    child.parent_relation.collect_videos_multiple_cats 
    if !child.children.blank? 
    self.get_graph(child, word_ary) 
    end 
    end 
    return word_ary 
end 

在圖形/ show.html.erb:

<script> 
$(document).ready(function() { 
    setInterval(function(){ 
    var idx = "<%= @graph.id %>"; 
    $.get("getstatus/", {idx}) 
     },1000); 
});  
    </script> 
<%= javascript_include_tag "pjs" %> 
<canvas id="graphsketch" data-processing-sources="/assets/pjs/graphBuilder2_2.pde"></canvas> 

在圖形/ getstatus.js.erb

變種文本= <%= array_or_string_for_javascript(@sentence_so_far)%>; var pjs = Processing.getInstanceById(「graphsketch」); alert(text); pjs.update(text);

警報在getstatus.js.erb永遠不會觸發。

在(當然)路線:

resource :graphs do 
    collection do 
    get 'getstatus' 
    end 
end 

我在做什麼錯?非常感謝再次幫助..

+0

getstatus.js.erb應該在app/views/graphs中,是嗎? – RadBrad 2012-04-01 00:35:16

+0

啊。只是實際上將format_js改爲format.js,一切正常。非常感謝! – Stpn 2012-04-01 00:36:11

+0

此外,我認爲對AJAX的調用應該更像 $ .get(「/ graphs/getstatus」);我總是使用jquery,所以我不是很熟悉javascript沒有jQuery! – RadBrad 2012-04-01 00:38:28

回答

1

讓我確保我明白了,你要顯示在它的整體圖形,因爲它的增長,刷新每3秒,正確嗎?

我首先要做的是建立在GraphController一個AJAX的行動,得到的圖形的全部內容爲止

def getstatus 
    @graph_so_far = Graph.??? 
    respond_to do |format| 
    format_js 
    end 
end 

當然你要添加新的路由

然後在getstatus.js.erb(與show.html相同的目錄中)。ERB)

var text = <%= array_or_string_for_javascript(@graph_so_far)%> ; 
var pjs = Processing.getInstanceById("graphsketch"); 
pjs.update(text); 

那得到的,你的第一次更新,現在你需要做的設置定時器,並揭開序幕相同AJAX調用3秒鐘後,我用jquery.timers.js,但不管,經過3秒將另一個AJAX請求發送到URL/graph/getstatus(或任何你稱之爲路由),再次開始循環。

+0

對不起,您還必須進行初始AJAX調用才能啓動事件,您只需在DOM加載事件之後發送AJAX請求到URL/graph/getstatus即可。 – RadBrad 2012-03-31 22:34:22

+0

謝謝!我現在就試試..我不太明白 - 什麼是「???」,你能解釋一下嗎? – Stpn 2012-03-31 23:01:14

+0

哦,我想這是模型調用..我需要休息。 – Stpn 2012-03-31 23:07:06