Coffeescript 认购';当光标未就绪时调用s onReady
我正在将一些非光标文本数据从Meteor服务器发布到订阅该数据的Meteor客户端。客户端的订阅位于Deps.autorun函数中,当因变量更改时,将成功调用订阅 然而,我想在文本数据一进来就解析它,所以我想我应该在subscribe的onReady函数中这样做。这在第一次发送数据时可以正常工作,但在随后发布不同的数据时,onReady函数仍然使用以前的数据 我想知道在数据完全通过后如何解析它 请参见下面的代码和输出示例: (为了完整性:我在Windows 8.1上使用Meteor 0.8.1.3,客户端是Chrome 35) 客户:Coffeescript 认购';当光标未就绪时调用s onReady,coffeescript,meteor,Coffeescript,Meteor,我正在将一些非光标文本数据从Meteor服务器发布到订阅该数据的Meteor客户端。客户端的订阅位于Deps.autorun函数中,当因变量更改时,将成功调用订阅 然而,我想在文本数据一进来就解析它,所以我想我应该在subscribe的onReady函数中这样做。这在第一次发送数据时可以正常工作,但在随后发布不同的数据时,onReady函数仍然使用以前的数据 我想知道在数据完全通过后如何解析它 请参见下面的代码和输出示例: (为了完整性:我在Windows 8.1上使用Meteor 0.8.1.
cursor = new Meteor.Collection "data"
reactiveInput = 0
dep = new Deps.Dependency
window.setReactiveInput = (newVal) ->
dep.changed()
reactiveInput = newVal
getReactiveInput = ->
dep.depend()
reactiveInput
Deps.autorun ->
Meteor.subscribe "loadData", getReactiveInput(), (r) ->
console.log "data ready"
for o, i in cursor.find().fetch()
console.log i, o.value
# Returns correct data on first call
# Returns original data on second call
# Returns second data on third call, etc...
Deps.autorun ->
Meteor.call "loadData", getReactiveInput(), (e, result) ->
console.log "data ready"
for o, i in result
console.log i, o.value
服务器:
Meteor.publish "loadData", (input) ->
time = Date.now()
for x in [0..parseInt(input)]
@added "data", x, {value: time}
@ready()
Meteor.methods
loadData: (input) ->
result = []
time = Date.now()
for x in [0..parseInt(input)]
result.push {value: time}
return result
样本输出:
data ready
0 1402208546540
> setReactiveInput(1)
1
data ready
0 1402208546540
1 1402208551687
> setReactiveInput(2)
2
data ready
0 1402208551687
1 1402208551687
2 1402208553949
因为我不需要实际的光标数据,我想我可以简单地调用Meteor方法来获得所需的数据。然而,我仍然认为,在最初的情况下,游标的数据没有正确更新是很奇怪的 解决方案: 客户:
cursor = new Meteor.Collection "data"
reactiveInput = 0
dep = new Deps.Dependency
window.setReactiveInput = (newVal) ->
dep.changed()
reactiveInput = newVal
getReactiveInput = ->
dep.depend()
reactiveInput
Deps.autorun ->
Meteor.subscribe "loadData", getReactiveInput(), (r) ->
console.log "data ready"
for o, i in cursor.find().fetch()
console.log i, o.value
# Returns correct data on first call
# Returns original data on second call
# Returns second data on third call, etc...
Deps.autorun ->
Meteor.call "loadData", getReactiveInput(), (e, result) ->
console.log "data ready"
for o, i in result
console.log i, o.value
服务器:
Meteor.publish "loadData", (input) ->
time = Date.now()
for x in [0..parseInt(input)]
@added "data", x, {value: time}
@ready()
Meteor.methods
loadData: (input) ->
result = []
time = Date.now()
for x in [0..parseInt(input)]
result.push {value: time}
return result
以下是可以解释您的问题的原因: 在
onReady
中,预期的行为是看到客户端数据更新为添加的记录,而不是删除的记录。完整的解释是
这里有一个解决方法:在访问数据之前稍等一下,这样就可以解决取消订阅的问题
Tracker.autorun(function() {
Meteor.subscribe('myCollection', Session.equals('whatever', true), function onReady() {
Meteor.setTimeout(function() {
console.log(MyCollection.find({}).fetch());
}, 0);
});
});
同样的问题。很烦人。使用Meteor 0.8.3和WINDOWS进行测试-PREVIEW@0.0.8.Same尝试在if(subscriptionHandle.ready()){…}中使用光标而不是在onReady()回调中使用光标时出现问题。我已向Meteor团队提交了一个问题: