2013-01-06 55 views
1

我正在自動訂閱客戶端上的集合。我的代碼如下。我希望客戶有更新的信息,但它永遠不會更新。除非我刷新頁面,否則此問題將體現在按鈕從不被消息取代。爲什麼我的測試集合沒有被更新?

實際上,我爲'Tests'集合創建的一個文檔應該在客戶端上將其testCount屬性更新爲1,但除非刷新頁面,否則它不會發生。它不應該更新嗎?

未安裝自動發佈軟件包。

在這一點上,我相信流星有一個錯誤,或者我只是不明白一些基本的東西。我即將安裝meteor.js的開發版本並找出它,或者放棄,直到Meteor.js實際上可以工作。 :P

我的控制檯日誌:

Tests updated! 
checking test 
no test 
Tests updated! Object {_id: "ea9f6002-74c7-4f37-9f10-0167b3b6f65a", testCount: 0} 
checking test result 
test.testCount 0 

我的服務器日誌:

$ meteor reset;meteor 
Project reset. 
[[[[[ ~/Dropbox/projects/sphela-game/sphela ]]]]] 

Running on: http://localhost:3000/ 
inserting new test 
Connecting test, getting initial data. 
Test added do nothing { testCount: 0, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' } 
Connecting test, getting initial data. 
Test added do nothing { testCount: 0, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' } 
Running test. 
Updating test to 1. 
Test ran. { testCount: 1, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' } 
Test added do nothing { testCount: 1, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' } 

我的HTML:

<head> <title>Testing counts.</title> </head> 

<body> {{> app}} </body> 

<template name="app"> 
    {{#if testSuccess}} 
    <h1>Test complete.</h1> 
    {{else}} 
    <button class="btn run-test">Run Test</button> 
    {{/if}} 
</template> 

我的javascript:

var Tests = new Meteor.Collection('tests'); 

if (Meteor.isClient) { 
    Meteor.startup(function() { 
    Session.set('testCount', 0) 
    Meteor.subscribe('connect'); 
    }); 
    Template.app.testSuccess = function() { 
    var test; 
    console.log('checking test result'); 
    test = Tests.findOne(); 
    if (!test) { 
     console.log('no test', test); 
     return false; 
    } 
    console.log('test.testCount', test.testCount); 
    return test.testCount > 0; 
    }; 
    Template.app.events({ 
    'click .run-test': runTest 
    }); 
    function runTest(event) { 
    Meteor.call('runTest'); 
    Session.set('testCount', 1); 
    } 
    Meteor.autorun(function() { 
    console.log('Tests updated!', Tests.findOne()); 
    }); 
    Meteor.autosubscribe(function() { 
    Meteor.subscribe('test-results', Session.get('testCount')); 
    }); 
} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    test = Tests.findOne({}) 
    if (!test) { 
     test = { 
     testCount: 0 
     }; 
     console.log('inserting new test'); 
     test._id = Tests.insert(test); 
    } else { 
     console.log('startup reset'); 
     test.testCount = 0; 
     Tests.update({_id:test._id}, test); 
    } 
    }); 

    Meteor.publish('connect', function() { 
    var test_; 
    console.log('Connecting test, getting initial data.'); 
    test_ = Tests.findOne({}); 
    this.set('tests', test_._id, test_); 
    this.complete(); 
    this.flush(); 
    }); 

    Meteor.publish('test-results', function(test) { 
    var handle; 
    handle = Tests.find({testCount: test}).observe({ 
     changed: _.bind(function(test) { 
     console.log('Test changed', test._id, test.testCount); 
     this.set('tests', test._id, test); 
     this.flush(); 
     }, this), 
     added: _.bind(function(test) { 
     console.log('Test added do nothing', test); 
     this.flush(); 
     }, this) 
    }); 
    this.complete(); 
    this.flush(); 
    this.onStop(function() { 
     handle.stop(); 
    }); 
    }); 
    Meteor.methods({ 
    runTest: function() { 
     var test; 
     console.log('Running test.'); 
     test = Tests.findOne({}); 
     test.testCount = 1; 
     console.log('Updating test to 1.'); 
     Tests.update({_id: test._id}, test); 
     console.log('Test ran.', Tests.findOne()); 
    }, 
    }); 
    } 

回答

3

流星中的Naomi在Google Group郵件列表中爲我回答了我的問題。我的問題的要點是兩個訂閱/發佈返回衝突的結果集,一個被忽略。

這實際上是在documenation,我錯過了:

如果不止一個訂閱發送一個 屬性相沖突的值(同一集合名稱,文件ID和屬性名), 那麼值在客戶端將從第一個訂閱 的客戶端激活。 (即使它不是第一個發送 複製的屬性。)

答案是在多個不返回結果集發表聲明,互相沖突。我想避免發回相同的收藏會有助於避免這種情況。

Naomi的完整的答案低於[1]:

你看到的行爲進入編寫定製的基於 出版商在流星觀測的某些細節。

在我看來,那是你所看到的是 多個訂閱如何工作的結果。這是我認爲正在發生的事情。 首次發佈,服務器發送下來「在收集測試中,有一個 對象ID爲ea9f6002-74c7-4f37-9f10-0167b3b6f65a其中testCount是 0」 Session變量數爲0,所以我們同意測試結果,其中 testCount爲0 。太好了,我們的數據庫的本地版本已經 認爲,所有預訂的同意,沒有改變是必要的。會議 變量計數被更新爲1 Autosubscribe去和訂閱 到測試結果testCount爲1。這意味着現在我們有兩個 出版商打算:「連接」認爲 ea9f6002-74c7-4f37-9f10-0167b3b6f65a有testCount爲0(它永遠不會 得到更新,它怎麼會有不同的想法?'test-results'得到一個 'added'消息說ea9f6002-74c7-4f37-9f10-0167b3b6f65a有一個 testCount爲1(當你運行新的觀察,你會得到一個「添加」的消息 在這一切之後,你得到一個「變」的消息時 東西相匹配的光標更改,) - 但它確實沒有與 增加的消息看起來像你期待着一個'改變'的信息改爲。這裏的總體結果是,由於我們有一個發佈者 稱ea9f6002-74c7-4f37-9f10-0167b3b6f65a的testCount爲0,而 沒有發佈者對此做任何其他說明,客戶認爲 ea9f6002-74c7-4f37-9f10- 0167b3b6f65a具有0

一個testCount爲了確保客戶看到的是,testCount是非0,所有的 出版商的收集必須說,在主張有關文檔 ea9f6002-74c7-4f37-9f10-0167b3b6f65a什麼 testCount是非0℃,用適當的一組和齊平的呼叫。即使 發送的消息testCount在添加的回調中爲 觀察到,流星不保證什麼時候客戶端看到 當它訂閱兩個不同的東西衝突關於文檔的 價值。

tl; dr版本:即使 項目已存在於數據庫中,觀察遊標也會在觀察集合中的所有內容都獲得一次追加回調。當你有多個出版社出版 在同一文件相同的密鑰,客戶端 看到的版本將是其中之一,但流星不保證爲 哪一個,如果出版商不同意。儘量不要讓多個 出版商不同意有關的文件的內容,它只能 造成混亂。

[1] https://groups.google.com/forum/?fromgroups=#!topic/meteor-talk/KBhXK6a44kY

1

此問題體現在一個按鈕永遠不會被 消息取代,除非我刷新頁面。

測試完成:

當運行代碼,點擊按鈕,該按鈕,然後用替換。

和控制檯顯示:

checking test result testcount.js:10 
test.testCount 1 

我粘貼你的JavaScript文件到我的IDE後發現一個錯誤;在runTest()方法的結束括號後的逗號。但是,據我所知,在運行測試時沒有負面影響。

+0

你有安裝包自動發佈? –

+0

完全忘了它。刪除它後,我的體驗與你一樣。 –

+0

那麼這是一個錯誤呢? –

相關問題