Correct way to build components


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.


(Robin Ward) #2

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 that is injected into your component to fetch the model is probably the accepted way to do it.


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

(David Taylor) #4

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.

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