Chat is a backend-oriented component and expects remote data loaded by URL:
view: "chat", url: "remote/data"
Chat uses the following services to work with data:
1. Local Data
const local = $$("chat").getService("local");
const chats = local.chats(); // returns chats collection
2. Operations
const ops = $$("chat").getService("operations");
ops.addMessage(cid, origin, text); // adds a new message
3. Backend
const back = $$("chat").getService("backend");
back.users().then((data) => console.log(data));
4. Upload
5. Helpers
const helpers = $$("chat").getService("helpers");
helpers.dateChatFormat(date); // sets new date format
Study the models folder in the source code for method signatures.
All loaded chats are stored in DataCollection. To access the collection 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
});
Each chat object has the following fields:
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]
}
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
});
A single user object has the following fields:
User object
avatar: "remote/avatars/1.jpg"
email: "alex@brown.com"
id: 1
name: "Alex Brown"
status: 2
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,
type: 901
}