re-frame: график nvd3 не отвечает на обновление подписок его компонента

Я использую инфраструктуру re-frame cljs, которая использует reagent в качестве своей библиотеки представлений. У меня есть компонент графа nvd3, который я хочу обновлять при обновлении его подписок.

К сожалению, граф никогда не обновляется после первоначального вызова :component-did-mount. :component-will-update никогда больше не вызывается после первоначального рендеринга.

Я хочу, чтобы график обновлялся, поскольку подписка уведомляет компоненты о том, что она слушает изменения.

Вот компонент графа-контейнера:

(defn weight-graph-container
  []
  (let [weight  (subscribe [:weight-change])
        bodyfat (subscribe [:bodyfat-change])
        weight-amount (reaction (get @weight :amount))
        weight-unit   (reaction (get @weight :unit))
        bf-percentage (reaction (get @bodyfat :percentage))
        lbm           (reaction (lib/lbm @weight-amount @bf-percentage))
        fat-mass      (reaction (- @weight-amount @lbm))]
    (reagent/create-class {:reagent-render weight-graph
                           :component-did-mount (draw-weight-graph @lbm @fat-mass "lb")
                           :display-name "weight-graph"
                           :component-did-update (draw-weight-graph @lbm @fat-mass "lb")})))

Вот компонент графика:

(defn draw-weight-graph [lbm fat-mass unit]
  (.addGraph js/nv (fn []
                     (let [chart (.. js/nv -models pieChart
                                     (x #(.-label %))
                                     (y #(.-value %))
                                     (showLabels true))]
                       (let [weight-data [{:label "LBM" :value lbm} {:label "Fat Mass" :value fat-mass}]]
                         (.. js/d3 (select "#weight-graph svg")
                                   (datum (clj->js weight-data))
                                   (call chart)))))))

Наконец, вот компонент, в который отображается график:

(defn weight-graph []
  [:section#weight-graph
   [:svg]])

Что мне не хватает? Спасибо за любую помощь.


person Kurt Mueller    schedule 20.07.2015    source источник


Ответы (1)


Следующий код решает вашу проблему:

(defn draw-weight-graph
  [d]
  (let [[lbm fat-mass unit] (reagent/children d)]
    (.addGraph js/nv (fn []
                       (let [chart (.. js/nv -models pieChart
                                       (x #(.-label %))
                                       (y #(.-value %))
                                       (showLabels true))]
                         (let [weight-data [{:label "LBM" :value lbm} {:label "Fat Mass" :value fat-mass}]]
                           (.. js/d3 (select "#weight-graph svg")
                               (datum (clj->js weight-data))
                               (call chart))))))))

(def graph-component (reagent/create-class {:reagent-render weight-graph
                                            :component-did-mount draw-weight-graph
                                            :display-name "weight-graph"
                                            :component-did-update draw-weight-graph}))

(defn weight-graph-container
  []
  (let [weight  (subscribe [:weight-change])
        bodyfat (subscribe [:bodyfat-change])
        weight-amount (reaction (get @weight :amount))
        weight-unit   (reaction (get @weight :unit))
        bf-percentage (reaction (get @bodyfat :percentage))
        lbm           (reaction (lib/lbm @weight-amount @bf-percentage))
        fat-mass      (reaction (- @weight-amount @lbm))]
  (fn []
     [graph-component @lbm @fat-mass "lb"])))
person ducky    schedule 20.07.2015
comment
Спасибо. Я изменил код на :component-did-update. - person Kurt Mueller; 21.07.2015