Search code examples
ember.jsember-climonkeypatching

Override addon function


I'd like to override a method inside the DockYard ember-changeset-validations addon so I can translate the error messages via I18n. This is the file where the function is located: https://github.com/DockYard/ember-changeset-validations/blob/master/addon/utils/get-messages.js

Is there any way I can override this getMessages() method without having to fork the whole repo?


Solution

  • ember-cli use define/require pair. So you can somewhere in initializer put code

    let oldImplementation = require('ember-changeset-validations/utils/get-messages').default;
    define('ember-changeset-validations/utils/get-messages', ['exports', 'ember', 'ember-changeset-validations/utils/messages'], function (exports, Ember, defaultMessages) {
      // override module implementation here
      exports['default'] = oldImplementation;
    });
    

    In such way you'll redefine module ( so you should keep module's interface the same ). But it's ugly hack