I’m looking for a way to extend Ember’s Object to include some additional methods, so that they become available every object (View, ArrayController, etc) in my app.
Specifically, I want to define some methods that introduces some naming conventions of the controllers, models, views, templates, helpers, etc.
For example:
If the class name of the View is ArticlesListView then its associated model is Article, the controller action is named list within ArticlesController, the template is in app/articles named list.js.hjs…
The end result should be, for example, App.ArticlesListView.model() would return App.Article.
So how do I extend the core Ember Object?
Ember.Object.extend({ // <--- ???
model: function(context, params){
}
});
The answer to the general question of enhancing an existing object is to use
reopen:As to your more specific question, that is more challenging. An object doesn’t typically know about the name of the property it is assigned to. You might be able to achieve your goal by traversing the properties of your namespaces (including
App) and find the one that matches the current class. You could cache that property name for future performance.Another approach would be to define a helper method for defining new models, controllers, etc. which you pass the name into. The method could handle creating the subclass, assigning it to a property of App, and setting an instance variable with the name.