【发布时间】:2018-06-30 06:07:37
【问题描述】:
我的 elm 应用程序使用自动滚动功能,它获取元素的 Y 位置并使用Dom.Scroll.toY 在那里滚动。
两个这样做,我设置了两个端口;订阅和发件人。
ports.elm
port setYofElementById : Maybe String -> Cmd msg
port getYofElementById : (Value -> msg) -> Sub msg
index.html
app.ports.setYofElementById.subscribe(function(id) {
var element = document.getElementById(id);
var rect = element.getBoundingClientRect();
app.ports.getYofElementById.send({"number": rect.top});
})
监听器是订阅
subscriptions : Model -> Sub Msg
subscriptions model =
Ports.getYofElementById getYofElementById
getYofElementById : Decode.Value -> Msg
getYofElementById value =
let
result =
Decode.decodeValue bSimpleIntValueDecoder value
in
case result of
Ok simpleIntValue ->
SetSelectedElementYPosition (Just simpleIntValue.number)
Err id ->
SetSelectedElementYPosition Nothing
SetSelectedElementYPosition 只是设置模型。
现在,执行此操作的操作做了两件事:调用 Port.setYofElementById,然后滚动到模型中的 Y 值,假设它已经设置好了。
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
ScrollToY idString ->
model
=> Cmd.batch
[ Ports.setYofElementById (Just idString)
, Task.attempt (always NoOp) <| Dom.Scroll.toY "ul" model.selectedElementYPosition
]
但是,这不会按顺序发生。当动作第一次触发时,什么也没有发生。如果我再次触发它,它会滚动到第一个动作中要求的位置。所以它似乎在设置值之前调用了Dom.Scroll.toY。
有没有办法强制ScrollToY 中的Cmds 依次发生?还是有更好的方法来做到这一点?
【问题讨论】:
标签: elm