2017-02-08 39 views
0

我一直在嘗試使用Dragula庫爲我的Meteor應用程序添加拖放功能。它看起來非常簡單易用。但是,即使我遵循了指示並查看了網絡上的其他示例,但我無法使其工作。它不會生成任何實際的錯誤,但我想移動的<div>元素不能移動。如何讓它工作:在流星上的Dragula拖放

JS:

import { Template } from 'meteor/templating'; 
import './body.html'; 

if (Meteor.isClient) { 

    Meteor.startup(function() { 
    Session.set("view", "titleScreen"); 
    }); 

    Template.body.helpers({ 
    template_name: function() { 
     return Session.get("view"); 
    } 
    }); 
    //click event to change view from title screen to main screen 
    Template.body.events({ 
    'click .changeScreen': function (e) { 
     e.preventDefault(); 
     Session.set("view", "mainScreen"); 
     var selectedView = Session.get('view'); 
     console.log(selectedView); 
    } 
    }); 
    //drag and drop the contents of divs 'move1' and 'goal1'? 
    Template.body.onRendered(function() { 
    dragula([document.getElementById('move1'), document.getElementById('goal1')]); 
    }); 
} 

HTML:

<body> 
    {{> Template.dynamic template=template_name}} 
</body> 
<template name="plcHolder"> 
    {{view}} 
    {{#if view "title"}} 
     {{> titleScreen}} 
    {{else}} 
     {{> mainScreen}} 
    {{/if}} 
</template> 
<template name="titleScreen"> 
<!--here would be the contents of the title screen, like some text and a button--> 
</template> 
<template name="mainScreen"> 
    <div class="container"> 
    <div id="goal1" class="goalBoxBG"> 
    <div class="goalBox"></div></div> 
<!--...--> 
    <div id="move1" class="moveBoxBGL"> 
    <div class="moveBox"></div></div> 
<!--...--> 
    </div> 
</template> 

這是我第一次流星的應用程序,所以我決定使用由流星的教程中使用相同的結構,即JS和上面提到的HTML文件放在../imports/ui/中並從那裏導入。我用npm安裝了Dragula,dragula.js文件位於\ node_modules \ dragula中。

編輯:我是能夠使通過移動代碼的HTML文件的工作進行到底,所以最主要的原因一定是在其中執行代碼的順序。它seemsonRendered()在頁面最初加載後觸發,並且不考慮由JavaScript更改的模板。

+0

您似乎沒有在您的代碼中導入Dragula。 – Mikkel

+0

@Mikkel我增加了一個import語句'/ node_modules/dragula/dragula.js',但它並沒有真正改變局面。不過,該文件顯示在瀏覽器的調試器中。 – JohnTheAsker

回答

1

導入語句不應引用節點模塊目錄。見

https://guide.meteor.com/using-npm-packages.html#client-npm的細節,基本上你應該只寫

import dragula from 'dragula'; 

而且包裝系統將努力去找到它。

有/是在大氣中的包:

https://atmospherejs.com/ahref/dragula

這確實導入你的工作,但筆者建議使用NPM作爲前進的方向。

+1

感謝您的信息!部分問題似乎與執行順序有關,但找到了一個臨時解決方案,因此它現在可以工作。 – JohnTheAsker