Ember | Subs
// Subscribe this.eventBus.on('data-updated', this, this.handleUpdate);
Only use subscriptions when data enters your app . 3. Subscribing to External Data (Best Practice) Step 1: Create a service // app/services/price-feed.js import Service from '@ember/service'; import tracked from '@glimmer/tracking'; import action from '@ember/object'; export default class PriceFeedService extends Service @tracked currentPrice = null; @tracked isConnected = false; ember subs
#if this.priceFeed.isConnected Current price: this.priceFeed.currentPrice else Connecting... /if ✅ No manual observers – just tracked properties and lifecycle hooks. 4. Subscriptions with Ember Data (Live Queries) If your backend pushes changes via WebSockets, update the store directly: // Subscribe this
disconnect() this.socket?.close();
constructor() super(...arguments); this.priceFeed.connect(); // Subscribe this.eventBus.on('data-updated'
// Unsubscribe (important!) this.eventBus.off('data-updated', this, this.handleUpdate);