How different are Ember component and Discourse widget?

I’m working on a plugin that need some modal dialog, both Ember component and Discourse widget may implement it i konw after search on meta. The Ember component and Discourse widget both can be attach to plugin outlet, render html, handle event.

I can only find one topic about widget above, it just said Discourse widget can render faster. But i found the Discourse core and much other plugins they both using Ember component and Discourse widget. For example:

Anyone can tell me what’s the different?
Thanks.

1 Like

Components are broadly used across most of Discourse. Where widgets come in useful is on some of our most highly-trafficked elements, such as the topic post stream and the header with its multiple menus.

You’ll often see widgets used in plugins and themes to add to or modify existing widgets.

Take for example this widget in the discourse-calendar plugin:

https://github.com/discourse/discourse-calendar/blob/4e31880f9cf67ca4e04edce06245bdaea7a46875/assets/javascripts/discourse/widgets/going-button.js.es6

This is a button rendered in the original post of a topic. It needs to be a widget because the post itself is rendered as a widget for performance reasons.

BUT if you look on lines 11-17 of that file, you can see a handlebars template defined in the JavaScript via the hbs helper, and in that template are components that are rendered like {{i18n ...}}.

So generally, you can use components unless the UI element is already rendered as a widget. In that case use a widget. And if it makes sense, you can utilize existing components inside the widget.

7 Likes

Thank you for your answer! That’s exactly what I want to know.

2 Likes

Some rule of thumb on components/widgets choice:

  • if you are displaying a lot of it, you probably want to choose widgets for perf
  • widgets are more limited than components, and require more knowledge to get something working perfectly when building complex UI

So basically if perf is not a question and I can use it, I will prefer components. If perf is a question or if Im forced to (eg: using the plugin api to decorate another widget), I will probably use widgets.

9 Likes