在我的客戶端用戶界面我有一個窗體與不同的搜索標準,我想反應更新結果列表。搜索查詢被轉化成一個經典的minimongo選擇,保存在一個Session變量,然後我有觀察家做事的結果:流星minimongo動態光標
// Think of a AirBnb-like application
// The session variable `search-query` is updated via a form
// example: Session.set('search-query', {price: {$lt: 100}});
Offers = new Meteor.Collection('offers');
Session.setDefault('search-query', {});
resultsCursor = Offers.find(Session.get('search-query'));
// I want to add and remove pins on a map
resultCursor.observe({
added: Map.addPin,
removed: Map.removePin
});
Deps.autorun(function() {
// I want to modify the cursor selector and keep the observers
// so that I would only have the diff between the old search and
// the new one
// This `modifySelector` method doesn't exist
resultsCursor.modifySelector(Session.get('search-query'));
});
我怎麼能實現光標對象上這個modifySelector
方法?
基本上我覺得這個方法需要更新遊標的編譯版本,即selector_f
屬性,然後重新運行觀察者(不會丟失先前結果的緩存)。還是有更好的解決方案?
編輯:你們有些人誤解了我想要做的事。讓我舉一個完整的例子:
Offers = new Meteor.Collection('offers');
if (Meteor.isServer && Offers.find().count() === 0) {
for (var i = 1; i < 4; i++) {
// Inserting documents {price: 1}, {price: 2} and {price: 3}
Offers.insert({price:i})
}
}
if (Meteor.isClient) {
Session.setDefault('search-query', {price:1});
resultsCursor = Offers.find(Session.get('search-query'));
resultsCursor.observe({
added: function (doc) {
// First, this added observer is fired once with the document
// matching the default query {price: 1}
console.log('added:', doc);
}
});
setTimeout(function() {
console.log('new search query');
// Then one second later, I'd like to have my "added observer" fired
// twice with docs {price: 2} and {price: 3}.
Session.set('search-query', {});
}, 1000);
}
而不是'resultCursor.observe',你有沒有試過'resultCursor.observeChanges'?這隻會跟蹤結果集中的變化,這似乎是你想要的。雖然我不確定如果遊標本身(即遊標的查詢)發生變化,它是否會起作用。請參閱http://docs.meteor.com/#observe_changes –
'observe' vs'observeChanges'與我的問題無關,即關於更改*遊標查詢*的問題。 – mquandalle
我認爲你不需要'Deps.autorun'塊。如果你用'Offers.find(Session.get('search-query'))''創建一個遊標,我會認爲每次Session變量發生變化時,該遊標都會自動更新,因爲Session是被動的。然後改變'resultCursor.observe'的位置爲'resultCursor.observeChanges',並剪切最後7行。那樣有用嗎? –