What is the reason behind reopening the model class?

I’ve noticed the backup model defines a model then reopens it a few lines below in the same file, adding functions like find, start, cancel etc. I’m wondering what is the reasoning behind that? Why does the class need to be re-opened? I’m sure this is a valid reason… I just don’t know it :slight_smile:

const Backup = Discourse.Model.extend({
  destroy() {},
  restore() {}
});

Backup.reopenClass({
  find() {
  ...
  }
  // etc
});

See: discourse/backup.js.es6 at master · discourse/discourse · GitHub

3 Likes

Nice question, I was a bit confused about this too!

reopenClass is used to add static methods (or properties) to the class itself. I don’t think there is a way to define static method through Ember.Object.extend.

A bit more info on reopenClass and reopen: Reopening Classes and Instances - The Object Model - Ember Guides

8 Likes