Javascript /流星Android将对象返回模板 [英] Javascript / meteor android return objects to templates
问题描述
我的流星应用程序中包含以下代码
I have the following piece of code in my meteor app
if (Meteor.isClient) {
Meteor.startup(function(){
var onSuccess = function(acceleration){
alert( acceleration);
};
var onError = function(acceleration){
return "error";
}
var options = { frequency: 3000 }; // Update every 3 seconds
var getAcc = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
});
}
这样做是每3秒检索一次android手机加速度计数据,然后如果轮询成功,它将在警报中显示该对象。
What that does is retrieve the android phones accelerometer data every 3 seconds, and on a successful poll it will show the object in an alert. This works fine.
但是,我不希望此轮询代码包含在启动功能中。我想对执行此操作的时间进行更多控制
However, I don't want this polling code to be in the startup function. I want to have more control over when this is executed
我有一个模板,可以在其中显示加速度计值。我在上面的代码中更改了 onSuccess
方法以返回对象而不是发出警报(其余的启动代码是相同的):
I have a template where I want to display the accelerometer values. I changed the onSuccess
method in the code above to return the object instead of alerting (the rest of the startup code is the same):
var onSuccess = function(acceleration){
return acceleration;
};
我的模板如下:
Template.rawData.helpers({
acc: function(){
alert(getAcc);
return getAcc;
}
});
我希望发生的是将加速度计数据存储在 getAcc
在启动功能中,然后通过 acc
返回到我的网页。这似乎没有发生。模板中的警报
也不会发生
What I'm expecting to happen is for the accelerometer data to be stored in getAcc
in the startup function, but then to return it through acc
to my webpage. This does not seem to happen. The alert
in the template doesn't occur either
是否可以从外部访问这些cordova插件启动功能?我只是错误地在启动和模板部分之间返回了对象吗?
Is there a way to access these cordova plugins from outside of the startup function? Am I just incorrectly returning the objects between the startup and template sections?
我想我的另一个首要问题是:我不确定如何通过这些方式显示那些加速度计值模板,如果它们聚集在启动功能中,而不是从模板帮助器中收集到的
I guess my other overarching question is this: I'm not sure how to display those accelerometer values through a template if theyre gathered in the startup function, and not from a template helper
推荐答案
您需要在以下情况下进行模板更新数据改变。为此,请设置反应变量,该变量将通过回调进行更新。首先,安装软件包:
You need to have the template update reactively when the data changes. To do that set up a reactive variable that gets updated by the callback. First, install the package:
meteor add reactive-var
然后,在创建模板时,创建反应变量并开始查看回调:
Then, when the template is created, create the reactive variable and start watching the callbacks:
Template.rawData.onCreated(function() {
self = this;
self.rawValue = new ReactiveVar();
self.accWatchID = navigator.accelerometer.watchAcceleration(
function(acc) { self.rawValue.set(acc); }, function() {}, { frequency: 3000 }
);
});
您的模板助手随后可以返回反应变量的值,并且您的模板将随时更新更改:
Your template helper can then return the value of the reactive variable, and your template will be updated whenever it changes:
Template.rawData.helpers({
acc: function() {
return Template.instance().rawValue.get();
}
});
(请注意,在原始代码中,由于未调用警报,因此必须有一个
(Note that in your original code, since the alert wasn't being called, there must be a problem in your template. Does it have the right name?)
最后,当模板被破坏时,您应该停止回调:
Finally, you should stop the callback when the template is destroyed:
Template.rawData.onDestroyed(function() {
navigator.accelerometer.clearWatch(this.accWatchID);
});
请注意,我只是在此处输入该代码而不对其进行测试。如果无法立即使用,则可能需要对其进行微调。
Note that I've just typed that code here without testing it. You may need to fine tune it a little if it doesn't work straight away.
这篇关于Javascript /流星Android将对象返回模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!