Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f571fc9

Browse files
Added Middleware documentation in Misc Tutorials (#34)
1 parent 2b5e48c commit f571fc9

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

‎src/v3/SUMMARY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
* [Core](tutorial/core/README.md)
108108
* [Localization](tutorial/locale/README.md)
109109
* [Bus](tutorial/bus/README.md)
110+
* [Middleware](tutorial/middleware/README.md)
110111
* [Clipboard](tutorial/clipboard/README.md)
111112
* [Drag-and-Drop](tutorial/dnd/README.md)
112113
* [IFrames](tutorial/iframe/README.md)

‎src/v3/tutorial/application/README.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To create a new application package, run the following command inside your OS.js
2222
2323
## Metadata
2424

25-
The `metadata.json` file describes your application and contains a list of files that is required to load the it.
25+
The `metadata.json` file describes your application and contains a list of files that is required to load on application launch or on boot.
2626

2727
> [info] Remember to run `npm run package:discover` after you change your metadata.
2828
@@ -76,10 +76,15 @@ The `metadata.json` file describes your application and contains a list of files
7676
"^video\/(.*)"
7777
],
7878

79-
// Load these files when launching (usually generated with Webpack)
79+
// Load these files (usually generated with Webpack)
8080
"files": [
81+
// Load files on application launch
8182
"main.js",
82-
"main.css"
83+
"main.css",
84+
// Equivalent to passing simple string "/path/to/file.js"
85+
{"filename": "/path/to/file.js", "type": "preload"},
86+
// Load file on boot
87+
{"filename": "/path/to/file.js", "type": "background"}
8388
]
8489
}
8590
```

‎src/v3/tutorial/middleware/README.md‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
description: This tutorial will show you how to use middleware.
3+
full_title: Middleware Tutorial
4+
---
5+
6+
# Middleware Tutorial
7+
8+
This tutorial will show you how to use middleware.
9+
10+
Applications can register middleware to provide features and change the behavior of other applications.
11+
12+
## Usage
13+
14+
Register a callback to middleware defined by a group name:
15+
16+
```javascript
17+
const middlewareFunction = () => {};
18+
core.make('osjs/middleware')
19+
.add('middleware-group-name', middlewareCallback);
20+
```
21+
22+
Alternatively you can the global registration function:
23+
24+
```javascript
25+
import osjs from 'osjs';
26+
27+
const middlewareFunction = () => {};
28+
osjs.middleware('middleware-group-name', middlewareCallback);
29+
```
30+
31+
To get the list of registered middleware from a group:
32+
33+
```javascript
34+
const middlewareCallbacks = core.make('osjs/middleware')
35+
.get('middleware-group-name'); // => [middlewareFunction]
36+
```
37+
38+
## Example
39+
40+
The [File Manager application](https://github.com/os-js/osjs-filemanager-application) uses middleware to construct the edit (context) menu.
41+
42+
You can add your own callbacks to this middleware if you need to for example launch your own application from this menu with the selected file as an argument. The middleware group name the File Manager exposes for this is `osjs/filemanager:menu:edit`.
43+
44+
To add your own menu entries with this middleware, you first have to change your package metadata:
45+
46+
```json
47+
// metadata.json in our application
48+
{
49+
"files": [
50+
"main.js",
51+
"main.css",
52+
// Add this line
53+
{"filename": "middleware.js", "type": "background"}
54+
]
55+
}
56+
```
57+
58+
Then change the webpack config and add `middleware.js` as a new entry:
59+
60+
```javascript
61+
entry: {
62+
main: path.resolve(__dirname, 'index.js'),
63+
middleware: path.resolve(__dirname, 'middleware.js')
64+
}
65+
```
66+
67+
Now the middleware can be added in the `middleware.js` file. In this example a callback is added to provide extra menu options based on options from the other application:
68+
69+
```javascript
70+
import osjs from 'osjs';
71+
72+
osjs.middleware('osjs/filemanager:menu:edit', async (({file, isContextMenu}) => {
73+
// It should return an array of objects with `label`, `disabled` and `onclick`
74+
75+
if (isContextMenu) {
76+
// Add this item only in context menu
77+
return [{
78+
label: 'Open in my application',
79+
disabled: !file || !file.isFile,
80+
onclick: () => osjs.run('my-app-name', {file})
81+
}];
82+
}
83+
84+
// Don't add any item to the file manager's edit menu
85+
return [];
86+
}));
87+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /