Using Resource Templates

Resource templates can be passed using the resource middleware to any widget that has a resource property exposed with the resources middleware.

MyWidget.ts

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import { createResourceTemplate, createResourceMiddleware } from '@dojo/framework/core/middleware/resources';
  3. import MyResourceAwareWidget from './MyResourceAwareWidget';
  4. const resource = createResourceMiddleware();
  5. const factory = create({ resource });
  6. interface MyResourceType {
  7. value: string;
  8. }
  9. const template = createResourceTemplate<MyResourceType>({
  10. read: (request, controls) => {
  11. // use the request to get the required data
  12. // use the controls to populate the resource
  13. }
  14. });
  15. export factory(function MyWidget({ middleware: { resource }}) {
  16. return (
  17. <div>
  18. <MyResourceAwareWidget resource={resource({ template })}>
  19. </div>
  20. );
  21. });

Passing Initialization Options

Initialization options can be passed with any template created using the createResourceTemplateWithInit factory and are passed to the template’s init function to initialize the resource. The initOptions include an id used to identify the backing resource and optional data that can be added to the resource on creation.

MyWidget.ts

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import { createResourceTemplate, createResourceMiddleware } from '@dojo/framework/core/middleware/resources';
  3. import MyResourceAwareWidget from './MyResourceAwareWidget';
  4. const resource = createResourceMiddleware();
  5. const factory = create({ resource });
  6. interface MyResourceType {
  7. value: string;
  8. }
  9. const template = createResourceTemplate<MyResourceType>({
  10. read: (request, controls) => {
  11. // use the request to get the required data
  12. // use the controls to populate the resource
  13. }
  14. });
  15. export factory(function MyWidget({ id, middleware: { resource }}) {
  16. return (
  17. <div>
  18. <MyResourceAwareWidget resource={resource({ template, initOptions: { id, data: [{ value: 'foo'}, { value: 'bar'}] } } )}>
  19. </div>
  20. );
  21. });

Transforming Data

When a widget has been configured with to use resources middleware with a different data interface to the template a “transform” descriptor is required. The transform is an object keyed by a key of the resources middleware type that maps to a key of the resource template type.

MyWidget.ts

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import { createResourceTemplate, createResourceMiddleware } from '@dojo/framework/core/middleware/resources';
  3. import MyResourceAwareWidget from './MyResourceAwareWidget';
  4. const resource = createResourceMiddleware();
  5. const factory = create({ resource });
  6. interface MyResourceType {
  7. custom: string;
  8. }
  9. const template = createResourceTemplate<MyResourceType>({
  10. read: (request, controls) => {
  11. // use the request to get the required data
  12. // use the controls to populate the resource
  13. }
  14. });
  15. export factory(function MyWidget({ id, middleware: { resource }}) {
  16. return (
  17. <div>
  18. <MyResourceAwareWidget resource={resource({ template, transform: { value: 'custom' }, initOptions: { id, data: [{ custom: 'foo'}, { custom: 'bar'}] } })} />
  19. </div>
  20. );
  21. });