Prototypes with ember-cli

Using ember-cli and its ES6 compiler, how and where to define this so that it applies to all arrays in my application:

Array.prototype.move = function (old_index, new_index) {
  ....
};

      

+3


source to share


1 answer


You have several options.

  • Add the file to the directory vendor/

    with your extensions and include it in yours Brocfile.js

    like so:

    app.import('vendor/my-prototype-extensions.js');
    
          

  • Do this in the initializer.

    ember g initializer extensions
    
          

    Then app/initializers/extension.js

    add your extensions like so:

    export var initialize = function() {
      Array.prototype.move = function (old_index, new_index) {
        ....
      };
    }
    
    export default {
      name: 'extensions',
      initialize: initialize
    }
    
          



I personally prefer the initializer approach, as is done in the ember-cli ecosystem, so you have access to whatever is available there if you need it.

+5


source







All Articles