2014-09-24 66 views
0

TLDR:我想跟蹤Meteor Collection的依賴關係,找出爲什麼我的模板幫助程序沒有反應。爲什麼不是流星收藏行爲反應?

我一直在嘗試在Meteor中創建一個可反覆使用的清單組件,該組件可以在不同的模板中重用。

模板:

<template name="checklist"> 
    <ul> 
    {{#each items}} 
    <li> 
     <label> 
     <input 
      type="checkbox" 
      value="{{value}}" 
      checked="{{isChecked}}" 
      data-id="{{_id}}" 
     /> {{name}} 
     </label> 
     <span>&nbsp;&nbsp;&nbsp; Status: {{status}}</span> 
    </li> 
    {{/each}} 
    </ul> 
     {{checkedIds}} 
</template> 

的Javascript:

if (Meteor.isClient) { 

    var list; 

    /** 
    * 
    * Creates a Checklist instance, with a local collection that maintains the status 
    * of all checkboxes: 'checked', 'unchecked' or 'indeterminate' 
    * 
    */ 
    function createChecklist() { 

    var _checked = new Meteor.Collection(null), 

    check = function(id) { 
     return _checked.upsert({_id: id}, {_id: id, status: 'checked'}); 
    }, 

    getStatus = function(id) { 
     var item = _checked.findOne({_id: id}) 
     return item && item.status; 
    }, 

    isChecked = function(id) { 
     return _checked.find({_id: id, status: 'checked'}).count() > 0; 
    }, 

    getCheckedIds = function() { 
     return _checked.find({status: 'checked'}).map(function(doc){return doc._id}); 
    }, 

    toggle = function(id) { 
     if (isChecked(id)) 
     return uncheck(id); 
     else 
     return check(id); 
    }, 

    uncheck = function(id) { 
     return _checked.upsert({_id: id}, {_id: id, status: 'unchecked'}); 
    }; 

    return Object.freeze({ 
     'check': check, 
     'getCheckedIds': getCheckedIds, 
     'getStatus': getStatus, 
     'isChecked': isChecked, 
     'toggle': toggle, 
     'uncheck': uncheck 
    }); 
    } 

    Template.checklist.helpers({ 
    items: [ 
     {_id: 0, name: 'Item 1', value: 10}, 
     {_id: 1, name: 'Item 2', value: 20}, 
     {_id: 2, name: 'Item 3', value: 40}, 
     {_id: 3, name: 'Item 4', value: 20}, 
     {_id: 4, name: 'Item 5', value: 100}, 
     ], 
    isChecked: function() { 
     return list.isChecked(this._id); 
    }, 
    status: function() { 
     return list.getStatus(this._id); 
    }, 
    checkedIds: function() { 
     return EJSON.stringify(list.getCheckedIds()); 
    } 
    }); 

    Template.checklist.events({ 
    'change [type=checkbox]': function(e, tmpl) { 
     var id = e.target.dataset.id; 
     list.toggle(id); 
    } 
    }); 

    Template.checklist.created = function() { 
    list = createChecklist(); 
    } 
} 

您會注意到,checkedIds助手每當您選中一個框時都會進行被動更新。但status幫助程序未被動更新。

我想:

  1. 軌道的的_checked收集的依賴關係,以制定出如果status助手已添加的計算。

  2. 瞭解爲什麼這個幫手沒有被動更新。

如果任何人都可以幫助這些項目,我會很感激。

到目前爲止,我已經做了以下內容:

  1. 證實Deps.active = true狀態幫手內(其功能調用)
  2. 將下面的代碼status幫手內部檢查,如果它是無效時我勾選複選框(這是從來沒有失效):

    var comp = Deps.currentComputation; comp.onInvalidate(function() { console.track(); });

回答

1

_id在Mongo中作爲字符串存儲。

更改爲:

getStatus = function(id) { 
    var item = _checked.findOne({_id: String(id)}) 
    return item && item.status; 
}, 
+0

哈哈,我只是一個工作的同事表示這並回答我的問題時,你的答案彈出。感謝堆! – francisbyrne 2014-09-24 09:04:52