Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prototype extensions with ember-cli

Using ember-cli and its ES6 module transpiler how and where would one define this so that would apply to all Arrays in my App:

Array.prototype.move = function (old_index, new_index) {
  ....
};
like image 249
TrevTheDev Avatar asked Dec 30 '25 04:12

TrevTheDev


1 Answers

You have a few options.

  1. Add a file to the vendor/ directory with your extensions and include it in your Brocfile.js like so:

    app.import('vendor/my-prototype-extensions.js');
    
  2. Do it in an initializer.

    ember g initializer extensions
    

    Then in 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 it's being done in the ember-cli ecosystem so you have access to anything available there if you need it.

like image 129
jakecraige Avatar answered Jan 01 '26 21:01

jakecraige



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!