Why / when do you need to re-open classes in ember.js?

There are times when you use App.Model.reopenClass() in a model ( link ), and Ember manuals talk about App.Router.reopen() ( link ). From the Ember Guides:

reopen is used to add instance methods and properties that are shared across all instances of the class. It does not add methods and properties for a specific instance of the class, as in vanilla JavaScript (without using a prototype).

But when you need to create class methods or add properties to a class, you can use reopenClass.

When is it necessary / profitable? Can't you just add everything right in front of you?

+7
source share
2 answers

reopenClass is similar to adding prototypes instead of adding methods for each instance of your classes. You can think of them as static variables / methods, rather than instance variables / methods.

This is a big gain in performance and may make more sense for the problem you are solving.

+3
source share

One example when you want to open a class is that if you want to add properties to an existing class by default. For example: many instances of ember applications do not extend the class of the router. They just use the default router class. But what if you want to add some properties to the class of the router that you want to use somewhere else. rediscovering is quite useful. This is one case that I can think of.

Also, in the case of extending the router, the class is difficult because most of the code in ember simply uses the router class. Even if you expand, some callbacks / closures will still apply to the earlier class of the router.

+1
source share

All Articles