# Correct way to build components

**URL:** https://meta.discourse.org/t/correct-way-to-build-components/55407
**Category:** Development
**Created:** [January 9, 2017, 1:28pm UTC](https://meta.discourse.org/t/correct-way-to-build-components/55407 "2017-01-09T13:28:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![piratdavid](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@piratdavid](https://meta.discourse.org/u/piratdavid)
#### Post date: [January 9, 2017, 1:28pm UTC](https://meta.discourse.org/t/correct-way-to-build-components/55407/1 "2017-01-09T13:28:10Z")

</div>

Here’s the structure of a component that I’m presenting in a News feed plugin on a page that’s not tied to the forum part of Discourse. My question is: Am I doing it right?

A controller that creates the components model and stores it as a variable on itself.  
A model that fetches its own data with ajax on creation and start a subscription on the messagebus that is passed on from the constructor.  
A component that receives its model from the creation in markup. It cancels the subscription on willDestroyElement and reinitiates it on didInsertElement.  
A template representing the markup.

I’m not sure if this is the right way to go. Is it right to pass the model this way? Should a component have a model? Should the model fetch it’s own data? It is not as self contained as I would have thought a component should be. Should it even be implemented as a component?

Hope to get some clarity in this.

Thanks  
David

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [January 9, 2017, 4:39pm UTC](https://meta.discourse.org/t/correct-way-to-build-components/55407/2 "2017-01-09T16:39:49Z")

</div>

One thing I’ve never been 100% clear in Ember is how they want you to fetch data for widgets that aren’t related to the rest of the content. I think fetching the data the way you suggest is probably fine. I think maybe using an [Ember Service](https://guides.emberjs.com/v2.10.0/applications/services/) that is injected into your component to fetch the model is probably the accepted way to do it.

---

<div class="post-metadata">

### Author: ![piratdavid](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@piratdavid](https://meta.discourse.org/u/piratdavid)
#### Post date: [January 10, 2017, 9:01am UTC](https://meta.discourse.org/t/correct-way-to-build-components/55407/3 "2017-01-10T09:01:07Z")

</div>

Yeah it’s not perfectly clear where to put stuff in Ember. But thanks! Gonna have a look at Services.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [January 10, 2017, 5:01pm UTC](https://meta.discourse.org/t/correct-way-to-build-components/55407/4 "2017-01-10T17:01:25Z")

</div>

I am far from being even vaguely knowledgable about Ember, but I did have a similar problem to you - a component that needed persistent data unrelated to the page content.

> [@Using Ember Services in Discourse plugins](https://meta.discourse.org/t/using-ember-services-in-discourse-plugins/51713):
>
> I am attempting to make use of Ember “[Services](https://guides.emberjs.com/v1.13.0/understanding-ember/dependency-injection-and-service-lookup/)” in a discourse plugin. Right now I am just trying to get a simple proof of concept working based off of [this tutorial](http://www.programwitherik.com/ember-services-tutorial/) online. Unfortunately I can’t get it working and am not sure if it’s something to do with how plugins are loaded in discourse, or if I’m just doing something wrong. Any help would be appreciated! Right now the plugin is purely clientside, so the plugin.rb just has basic info. # name: Test Plugin # about: A plugin to test Ember ser…

I then used that for my plugin here - the source code might be useful for you:

> [@Discourse Who's Online](https://meta.discourse.org/t/whos-online-plugin/52345):
>
> discourse2Summary Discourse Who’s Online displays a list of users currently active on the sitehammer_and_wrenchRepository Link [https://github.com/discourse/discourse-whos-online](https://github.com/discourse/discourse-whos-online)open_bookInstall Guide [How to install plugins in Discourse](https://meta.discourse.org/t/install-plugins-in-discourse/19157)Featuresinformation_source Think carefully before installing this plugin. Discourse is designed for asynchronous discussion, and a list of online users can be harmful for some communities information_source This plugin is b…
