Working with Data

Loading Data

Chat is a backend-oriented component and expects remote data loaded by URL:

 view: "chat", url: "remote/data"

Jet Services

Chat uses the following services to work with data:

  • Local - working with client-side data;
  • Operations - synchronizes the backend data with the client data;
  • Upload - methods for uploading the avatars for chats;
  • Helpers - methods for view templates, dateFormats, etc;
  • Backend - sending loand a save requests to backend.

If you do not have a backend server or require custom logic, you can override the Backend service.

Data Operations

You can access any local DataCollection (charts, users) as well as the messages of any desired chat via the Local service.

If you need to access the data as soon as it is available, be sure to wait until it is available on the client. You can ensure it within the .ready handler of the Backend service.

const chat = $$("ch");
chat.getService("backend").ready().then(() => {
// accessing a collection
});

Getting chats

To access all the chats call the .chats() method of the Local service:

const chat = $$("ch");
chat.getService("backend").ready().then(() => {
  const chats = chat.getService("local")
  .chats() // getting the collection
  .serialize(); // serializing the collection to access data elements
});

The method returns a promise that resolves with the DataCollection of chats.

Each chat object has the following fields:

  • avatar (string) - the URL to load chat avatar from;
  • date (object) - date object;
  • direct_id (number) - the id of the user with whom a private chat is active;
  • id (number) - the chat id;
  • message ("string") - the latest message in the chat;
  • name (string) - the chat name;
  • unread_count (number) - number of the unread messages in the chat (from the current user perspective);
  • users (array) - an array of users of the chat.

Chat object

{
  avatar: "remote/avatars/members.jpg",
  chat_id: 0,
  date: new Date(),
  direct_id: 0,
  id: 19,
  message: "Hi there"
  name: "Today's meeting",
  unread_count: 2,
  user_id: 2,
  users: [3, 4, 1]
}

Getting users

To get the users collection call the users() method of the Local service:

const chat = $$("ch");
chat.getService("backend").ready().then(() => {
  const users = chat.getService("local")
  .users() // getting the collection
  .serialize(); // serializing the collection to access data elements
});

The method returns a promise that resolves with a users collection. A single user object has the following fields:

  • avatar (string) - URL to load user avatar from;
  • email (string) - the user email;
  • id (number) - the user id;
  • name (string) - the user name;
  • status (number) - the user status. Takes one of the following values:
    • 1 - "none",
    • 2 - "active",
    • 3 - "busy",
    • 4 - "away"

User object

  avatar: "remote/avatars/1.jpg"
  email: "alex@brown.com"
  id: 1
  name: "Alex Brown"
  status: 2

Getting messages

To access messages from a certain chat call the messages() method and pass the id of the chat as a parameter:

const chat = $$("ch");
chat.getService("backend").ready().then(() => {
  chat.getService("local")
  .messages(6) // getting an array of messages from the chat with "id: 6"
  .then(messages => {
    console.log(messages); // messages array
  });
});

Related sample:  Chat: Data Operations

The method returns a promise that resolves with an array of messages from the specified chat. Each message object has the following fields:

Message object

{
  chat_id: 6,
  date: new Date(),
  id: 19,
  text: "Who is on duty today?",
  user_id: 2
}
  • chat_id (number) - the chat id;
  • date (object) - date object;
  • id (number) - the id of the message;
  • text (string) - the text of the message;
  • user_id (number) - the id of the user the message was left by.
Back to top