mirror of
https://github.com/discourse/discourse.git
synced 2024-12-14 16:53:44 +08:00
DEV: Add support for api-initializers
to reduce boilerplate.
You can now create a file in your plugin/theme in the `api-initializers` directory which has a simpler template than previous initializers. Example: ``` // api-initializers/my-plugin.js import { apiInitializer } from "discourse/lib/api"; export default apiInitializer("0.8", api => { console.log("hello world from api initializer!"); }); ```
This commit is contained in:
parent
c8ed4e9868
commit
db8b8742c4
|
@ -154,7 +154,7 @@ const Discourse = Application.extend({
|
||||||
Object.keys(requirejs._eak_seen).forEach(key => {
|
Object.keys(requirejs._eak_seen).forEach(key => {
|
||||||
if (/\/pre\-initializers\//.test(key)) {
|
if (/\/pre\-initializers\//.test(key)) {
|
||||||
this.initializer(this._prepareInitializer(key));
|
this.initializer(this._prepareInitializer(key));
|
||||||
} else if (/\/initializers\//.test(key)) {
|
} else if (/\/(api\-)?initializers\//.test(key)) {
|
||||||
this.instanceInitializer(this._prepareInitializer(key));
|
this.instanceInitializer(this._prepareInitializer(key));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
18
app/assets/javascripts/discourse/app/lib/api.js
Normal file
18
app/assets/javascripts/discourse/app/lib/api.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* apiInitializer(version, apiCodeCallback, opts)
|
||||||
|
*
|
||||||
|
* An API to simplify the creation of initializers for plugins/themes by removing
|
||||||
|
* some of the boilerplate.
|
||||||
|
*/
|
||||||
|
let _apiInitializerId = 0;
|
||||||
|
export function apiInitializer(version, cb, opts) {
|
||||||
|
return {
|
||||||
|
name: `api-initializer${_apiInitializerId++}`,
|
||||||
|
after: "inject-objects",
|
||||||
|
initialize() {
|
||||||
|
return withPluginApi(version, cb, opts);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user