close

Filter

loading table of contents...

Studio Developer Manual / Version 2301

Table Of Contents

9.31.6 REST Linking (Studio RemoteBeans)

Since you have created another EntityController, you have to declare the matching remote beans the same way you already did for the Note remote bean. That means, you have to declare the interface

import RemoteBean from "@coremedia/studio-client.client-core/data/RemoteBean";
import Note from "./Note";

abstract class Notes extends RemoteBean {

  abstract getNotes():Note[];
}

export default Notes;

Example 9.136. Interface for remote bean for notes list


and the implementing class

import { mixin } from "@jangaroo/runtime";
import RemoteBeanImpl from "@coremedia/studio-client.client-core-impl/data/impl/RemoteBeanImpl";
import Note from "./Note";
import Notes from "./Notes";

class NotesImpl extends RemoteBeanImpl implements Notes {
  static readonly REST_RESOURCE_URI_TEMPLATE:string = "notes";

  constructor(uri:string) {
    super(uri);
  }

  getNotes():Note[] {
    return this.get("notes");
  }
}
mixin(NotesImpl, Notes);

export default NotesImpl;

Example 9.137. Implementing class for remote bean for notes list


and finally tell Studio that a new remote bean type is there:

BeanFactoryImpl.initBeanFactory().registerRemoteBeanClasses(NoteImpl, NotesImpl)

Example 9.138. Register remote bean with Studio


Rebuild and reload Studio. Once you are logged in, test the new REST controller manually by invoking the following URL in another browser tab: http://localhost:43080/rest/api/notes/ (the path may differ depending on your setup). As a result, you should see the following:

{
  notes: [
    {
      $Ref: "notes/note/1"
    },
    {
      $Ref: "notes/note/2"
    }
  ]
}

Example 9.139. Test result of remote bean


Note that not the plain JSON of the entities is serialized, but the references to them instead. For every class that is part of a representation a lookup is made if there is a corresponding EntityController declared for it. If true, the link to this controller is serialized instead of the linked entity.

Warning

Warning

The serialization and deserialization of entities consumed or produced by the EntityController is never handled by the controller itself. Please do not make any assumptions on how serialization and deserialization is implemented in your code, as this is not part of the Public API.

Invoke this inside TypeScript:

const notes = as(beanFactory.getRemoteBean("notes"), Notes);
notes.load((loadedNotes):void => {
  console.log('I have ${loadedNotes.getNotes().length} notes`);
});

Example 9.140. Invoke notes in TypeScript


The code looks similar to the previous example. The matching remote bean is created and loaded and the status of the bean is logged to the console. Note that only the Notes bean has been loaded through this code. The child elements must be loaded separately, so to display everything you can do something like this:

const notes = as(beanFactory.getRemoteBean('notes'), Notes);
notes.load((loadedNotes:Notes) => {
  console.log(`I have ${loadedNotes.getNotes().length} notes`);

  loadedNotes.getNotes()[0].load(note1 => console.log(note1.getDescription()));

  loadedNotes.getNotes()[1].load(note2 => console.log(note2.getDescription()));
});

Example 9.141. Display child elements of notes list


The output will look like this:

I have 2 notes
I have to write a real storage for this!
And a lot of other stuff too!

Example 9.142. Output of notes list


Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.