我正在使用 elm 0.17.1 并尝试与 select2 javascript 库(版本 4.0.3)进行互操作,这是我的 Main.elm :
port module Main exposing (..)
import Html exposing (Html,select,option,div,text,br)
import Html.App as App
import Html.Attributes exposing (id,value,width)
-- MODEL
type alias Model =
{
country : String
}
-- UPDATE
type Msg =
Select String
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Select str -> (Model str,Cmd.none)
-- VIEW
view : Model -> Html Msg
view model =
div[]
[
select [id "myselect"]
[
option [value "US"] [text "United States"],
option [value "UK"] [text "United Kingdom"]
],
text model.country
]
-- SUBSCRIPTIONS
port selection : (String -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions _=
selection Select
port issueselect2 : String -> Cmd msg
-- INIT
init : (Model, Cmd Msg)
init =
({country=""},issueselect2 "myselect")
main : Program Never
main = App.program {init=init,view=view,update=update,subscriptions=subscriptions}
和 JavaScript 方面:
$(document).ready(function()
{
var app=Elm.Main.fullscreen();
app.ports.issueselect2.subscribe(function(id)
{
$('#'+id).select2().on('change',function(e)
{
app.ports.selection.send(e.target.value);
});
})
})
现在,当我选择一个国家/地区时,我的 chromium 控制台中会出现未捕获的类型错误,说明domNode.replaceData
不是一个函数(它实际上是未定义的)。
问题是 select2 向 DOM 添加了一个跨度,而 Elm 不知道这一点,检查domNode
揭示 Elm 尝试更新span
何时应该更新文本。
我想我需要效果,但我不知道如何在我的特定用例中使用它们。
如何解决我的问题?
根据记录,我使用的是 jquery 3,我将 elm 程序编译到 main.js 中,并按以下顺序加载 js 文件:jquery.min.js、select2.min.js、main.js,然后是上面的 js 代码。
我无法使用 elm-reactor 进行调试,因为它似乎只适用于 elm 代码,而不适用于 js 代码。