簡體   English   中英

調用在剔除js ViewModel中定義的函數

[英]Calling a function defined within knockout js ViewModel

我有一個html 5視圖,想要執行拖放操作。 在我的html中,我有一個淘汰賽foreach,如下所示。 li元素被拖動。

<ul class="games-list" data-bind="foreach: games">
    <li data-bind="text: name, attr: { 'data-dragdrop': id }" 
        ondragstart='setTransferProperties(event)' 
        draggable="true">
    </li>
</ul>

這是帶有knockout ViewModel的javascript

<script type="text/javascript">

    var AppScope = function () {

        ...//plain js object here

        //knockout js View Model
        function PlayersViewModel() {
            var self = this

            self.games = ko.observableArray([
                                            new Game({ id: 0, name: "Cricket" }),
                                            new Game({ id: 1, name: "Football" }),
                                            new Game({ id: 2, name: "Hockey" })
                                            ]);
            ...
            //Drag and Drop
            self.setTransferProperties = function (event) { //Not invoked
                event.dataTransfer.setData("Text", event.target.getAttribute('data-dragdrop'));
            };
        }
    }

通過上面的操作,可以在AppScope中查找setTransferProperties(event) ,而不是在knockout ViewModel內部查找,因此找不到。

執行Drag時,調用淘汰模型ViewModel中定義的setTransferProperties(event)的方式是什么?

您需要執行以下操作

<ul class="games-list" data-bind="foreach: games">
    <li data-bind="text: name, attr: { 
                        'data-dragdrop': id ,
                        'ondragstart':$parent.setTransferProperties
        }" 
        draggable="true">
    </li>
</ul>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM