Can you describe your use case a little more? We use short-polling pretty heavily in one of our apps (switching to websockets soon). I believe a service is probably what you’re looking for for short or long polling solutions.
A helper should generally be a simple function that returns a value that is meant to be used in a template. For example ember truth helpers contains a bunch of helpers like “and” which just returns a boolean and of all passed values, or “not” which just returns the boolean inverse of the passed value. A more complicated helper example that we use is for permissions, aka you pass in two arguments like ‘read’ and ‘user’ and the helper returns a boolean telling you whether or not you have that permission so you can render a button or edit controls or something. This helper uses a service to determine the permissions:
{{#if (permission-to 'write' 'user')}}
...
{{/if}}
A service is useful for any piece of global data or state or functionality that you may need to use throughout your app, or control throughout your app, or just from the application route/controller. Here is a simplified short-poller example from our app (it uses raw ajax instead of ember data but pretty much the same thing, and you could shove the records into the store either way):
import $ from 'jquery';
import { bind, cancel, later } from '@ember/runloop';
import Service, { inject as service } from '@ember/service';
import ENV from '../config/environment';
export default Service.extend({
session: service(),
currentStats: null,
pollFrequency: 3000,
start: function(/*subscriber, */){
if(this.get("pollTimer") !== undefined){
cancel(this.get("pollTimer"));
}
this.statsPoll();
},
stop: function(/*subscriber*/){
cancel(this.get("pollTimer"));
},
statsPoll: function(){
let self = this;
$.ajax({
url: `${ENV.APP.API_HOST}/stats`,
type: "GET",
contentType: "application/json",
beforeSend: function (xhr) {
var token = self.get('session.session.authenticated.access_token');
if(token){
xhr.setRequestHeader ("Authorization", "Bearer " + token);
}
}
}).then(bind(this, response => {
this.set('currentStats', response);
if(ENV.environment !== "test") {
this.set("pollTimer", later(this, this.statsPoll.bind(this), this.get("pollFrequency")));
}
})).catch((/*error*/) => {
// make sure to keep the poller going even if there was an error
if(ENV.environment !== "test") {
this.set("pollTimer", later(this, this.statsPoll.bind(this), this.get("pollFrequency")));
}
});
}
});
EDIT: basic rules of thumb:
- If you need something that returns a single value to be used in a template, use a helper
- If you need something more complicated that either has some javascript bits and/or more than just a simple DOM element/value, use a component
- If you have global state or some kind of processing task that needs to happen in the global/application context (like polling) use a service