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); }); }

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

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; };

My template looks like this:

Template.rawData.helpers({ acc: function(){ alert(getAcc); return getAcc; } });

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

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 <a href="http://docs.meteor.com/#/full/reactivevar_pkg" rel="nofollow">reactive variable</a> 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.


  • grails unit test + Thread
  • Android - Are there query-able services that already poll for GPS location?
  • How to correctly configure interrupt for INT line of Atmel MXT641T touch chip in Android 5 kernel de
  • libusb and poll / select
  • UWP/C# - Issue with AQS and USB Devices
  • Can I programmatically choose the Android layout folder?
  • ASP.NET MVC Application won't update some controllers
  • Django invalid literal for int() with base 10
  • xtable package: Skipping some rows in the output
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Meteor helpers not available in Angular template
  • Highlight and Bold text in JTextPane
  • C++ Partial template specialization - design simplification
  • Q promise. Difference between .when and .then
  • Nant, Vault & Windows Integrated Authentication
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Bug in WPF DataGrid
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Incrementing object id automatically JS constructor (static method and variable)
  • Does CUDA 5 support STL or THRUST inside the device code?
  • ILMerge & Keep Assembly Name
  • When should I choose bucket sort over other sorting algorithms?
  • Hazelcast - OperationTimeoutException
  • How to make Safari send if-modified-since header?
  • Large data - storage and query
  • To display the title for the current loaction in map in iphone
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Akka Routing: Reply's send to router ends up as dead letters
  • WOWZA + RTMP + HTML5 Playback?
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app