Added custom types example with class constructor (#695)

This commit is contained in:
Stefanie Doll
2019-02-14 16:54:09 +01:00
committed by GitHub
parent d777b2ea17
commit f8f41b5ee3
+25 -7
View File
@@ -73,24 +73,42 @@ Registering custom types
Additional types used by runtime modules can be added when a new instance of the API is created. This is necessary if the runtime modules use types which are not available in the base Substrate runtime. Additional types used by runtime modules can be added when a new instance of the API is created. This is necessary if the runtime modules use types which are not available in the base Substrate runtime.
```javascript ```javascript
// The examples are valid for both versions of the api
import { ApiPromise } from '@polkadot/api'; import { ApiPromise } from '@polkadot/api';
// initialise via static create and register custom types // initialise via static create and register custom types
const api = await ApiPromise.create({ const api = await ApiPromise.create({
types: { types: {
CustomTypesExample: { CustomTypesExample: {
"id": "u32", 'id': 'u32',
"data": "Vec<u8>", 'data': 'Vec<u8>'
"deposit": "Balance",
"owner": "AccountId",
"application_expiry": "Moment",
"whitelisted": "bool",
"challenge_id": "u32"
} }
} }
}); });
``` ```
An alternative approach to register custom types is to define them as a Javascript class which you can then use in the constructor function of the api.
```javascript
// Define your struct in JS
class MyCustomTypes extends Struct {
constructor() {
super({
'id': 'u32',
'data': 'Vec<u8>'
});
}
}
// Then add it in the constructor
const api = new Api({
provider: /* some provider */,
types: {
MyCustomTypes: MyCustomTypes
}
});
```
Users Users
----- -----