我想让Om与 ScrollSpy 一起工作。
我目前正在使用此代码:
(defn main-component
[data owner]
(om/component
(dom/div
#js {:className "col-sm-10 col-sm-offset-2 col-md-10 col-md-offset-2 main"}
(dom/div
#js {:className "main-panel"
:data-spy "scroll"
:data-target ".nav-sidebar"}
"..."))))
(defn sidebar-component
[data owner]
(om/component
(dom/div
#js {:className "container-fluid"}
(dom/div
#js {:className "row"}
(dom/div
#js {:className "col-sm-2 col-md-2 sidebar"}
(dom/div
#js {:className "nav-sidebar"}
(dom/ul
#js {:className "nav"}
(dom/li nil (dom/a #js {:href "#thing-1"} "Thing 1"))
(dom/li nil (dom/a #js {:href "#thing-2"} "Thing 2"))
(dom/li nil (dom/a #js {:href "#thing-3"} "Thing 3"))
)))))))
(defn app-component
"The top-level Om component."
[data owner]
(om/component
(om/build
(common/app-template
(om/build sidebar-component data)
(om/build main-component data))
data)))
目前,它似乎一直有效,直到 Om / React.js 重新渲染 DOM。之后,ScrollSpy 停止工作。我想我知道为什么。根据Bootstrap ScrollSpy的说法:“当使用 scrollspy 并在 DOM 中添加或删除元素时,您需要像这样调用刷新方法:”
$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh');
});
我应该怎么办?我想我想连接到 Om 并告诉它调用 ScrollSpy,如上所示。