2014-11-23 43 views
0

我有一個可以移動的元素的集合。我收集的時間,他們上次搬家。流星 - 從jquery-ui可拖動的「開始」事件更新模板數據

Cards.find({},{sort:{moved:1}}); 

當我開始拖動我想更新moved屬性的元素,但我不希望我做拖動之前更新數據庫。

這是我的嘗試:

Template.cardItem.rendered = function() { 
    $('.card-item').draggable({ 
     handle: '.card-handle', 
     start: function(evt, ui) { 
      var data = Blaze.getData(evt.target); 
      data.moved = new Date().getTime(); // not working 
     }, 
     stop: function(evt, ui) { 
      var card = { 
       _id: $(this).attr('id'), 
       left: ui.position.left, 
       top: ui.position.top 
      }; 
      Meteor.call('cardMove', card, function(error, result) { 
       if (error) { 
        console.log(error); 
       } 
      }); 
     } 
    }) 
}; 

我想我的問題是,我不知道如何從一個jQuery的背景下訪問模板實例。

回答

0

我通常使用:Template.currentData()

也許你是對有關不能夠從一個jQuery場景中獲得的數據,我不知道這件事也。

這是使用會話或自定義ReactiveDict的完美場景,請參見documentation

你也可以這樣做:

this.autorun(function(computation){ 
    var data = Template.currentData(); //this is a reactive dependency 
    if(computation.firstRun){ 
     //init your jquery code 
    } 
}); 

而你在你的jQuery代碼訪問數據。爲什麼你需要創建一個自動運行並檢查第一次運行的原因是爲了避免每次運行jQuery代碼數據的變化。我有嚴重的性能問題,我是用Template.rendered時創建插件。

在你的情況,我認爲使用Session/ReactiveDict是要走的路。從長遠來看,它提供了更大的靈活性。

注意: 我不喜歡流星方法,我認爲這是針對流星工作流。這將是簡單的使用allow/deny規則,直接通過minimongo插入。

+0

什麼是'this'並在您的代碼示例'computation',並在是否應該把? – swenedo 2014-11-25 21:51:51

+0

'this'指向當前模板,例如:'this。$'表示您只在此模板上運行jQuery。這是一個很好的做法。 「計算」是允許訪問計算的參數。我使用的是'firstRun'屬性來檢查,如果這是第一次我正在運行'autorun'。 – 2014-11-25 23:40:38