Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember CLI: where to reopen framework classes

Tags:

I'd like to reopen Ember or Ember Data framework classes. Using Ember CLI, where is the right place to put these so that they get initialized property? Here's an example of something I'd like to do:

import DS from 'ember-data';  DS.Model.reopen({   rollback: function() {     this._super();     // do some additional stuff   } }); 
like image 749
Johnny Oshika Avatar asked Nov 26 '14 17:11

Johnny Oshika


People also ask

What can I do with the Ember CLI?

For example, you can use it to create components, routes, services, models, and more. For a full list, type ember generate --help. The CLI's generate command will ensure that new files contain the necessary boilerplate, that they go in the right directories, and that file naming conventions are followed.

What is the release schedule for the Ember CLI?

The CLI is backwards-compatible with older Ember apps and maintains a 6-week release schedule. The CLI was also built with the idea that a developer should be able to focus on building great apps, not re-engineering how to fit pieces together throughout an app's lifecycle.

How do I generate a help file in Ember?

For a full list, type ember generate --help. The CLI's generate command will ensure that new files contain the necessary boilerplate, that they go in the right directories, and that file naming conventions are followed. To avoid mistakes that are hard to debug, always use the CLI to create files instead of creating the files by hand.

What is Ember-CLI?

The Ember CLI (command line interface) is the official way to create, build, test, and serve the files that make up an Ember.js app or addon. Many things have to happen before any web app is ready for the browser, and the Ember CLI helps you get there with zero configuration. npm install -g ember-cli


1 Answers

I think the best way to execute modules that have side effects would be to create an initializer. Something like this:

// app/initializers/modify-model.js import DS from 'ember-data';  let alreadyRun = false;  export default {     name: 'modify-model',     initialize() {         if (alreadyRun) {             return;         } else {             alreadyRun = true;         }          DS.Model.reopen({             // ...         });     } }; 

Initializers are automatically run by Ember-CLI, so there's no need to call them yourself.

EDIT: As Karim Baaba pointed out, it's possible for initializers to run more than once. For an easy way around that, I've included an alreadyRun flag.

like image 196
GJK Avatar answered Oct 18 '22 14:10

GJK