Reputation: 13
This might be a broad question, but I'm looking at: Is there a part of the Ember API that provide some facility to create extensions? Any existing plugins that show a good approach to it?
Thanks!,
Upvotes: 0
Views: 329
Reputation: 27012
There is a lot built into Ember.js to extend functionality, as a lot of the classes can be extended, which is the standard way of building your own controllers, routes, and models; and existing classes, and existing instances can both be "reopened". [It might be better to ask specifically what you want to achieve for a more specific answer on a good way to go about doing it.]
Also, if you want to have a completely new class that defines behaviour that your controllers or routes need access to, then there is a mechanism where you can inject an instance of your new class into them, without the instance being a global variable. Based on the code from http://livsey.org/blog/2013/02/10/integrating-pusher-with-ember/ , you can define the class by:
Ember.MyNewPlugin = Ember.Object.extend({
init: function() {
// Anything you need to do to initialise the object
},
myFunc: function(param) {
// Anything you need to do with with "param"
}
});
And then inject the instance into controllers and routes by:
Ember.Application.initializer({
name: "mynewplugin",
initialize: function(container, application) {
// use the same instance everywhere in the app
container.optionsForType('mynewplugin', { singleton: true });
// register 'mynewplugin:main' as our MyNewPlugin object
container.register('mynewplugin', 'main', Ember.MyNewPlugin);
// inject the MyNewPlugin object into all controllers and routes
container.typeInjection('controller', 'mynewplugin', 'mynewplugin:main');
container.typeInjection('route', 'mynewplugin', 'mynewplugin:main');
}
});
Then in controllers and routes you can access the instance by
var myNewPlugin = this.get('mynewplugin');
And call its functions by:
myNewPlugin("Some data");
Upvotes: 3
Reputation: 11668
There is nothing special you need to in oder to add "extensions" to Ember. The most likely thing you will want to do is introduce new Controllers and Views
. In order to do so, you just have to subclass the core classes of Ember. Here is an example of such an "extension", which provides views, that are reusable and provide lots of value.
Upvotes: 0