@@ -46,77 +46,78 @@ import kuzzle from "./services/kuzzle";
4646
4747export default {
4848 name: " app" ,
49- /* snippet:start:4 */
49+ /* snippet:start:4 */
5050 data () {
5151 return {
52- message: " " , // The string containing the user input
53- messages: [], // The array containing our messages
54- roomID: " " , // The Id of the realtime subscription
55- username: " " , // The pseudo of the current user
56- validate: false // The value that will change the display (false => Pseudo input; true => Message input)
52+ message: " " , // String containing the user input
53+ messages: [], // Array containing our messages
54+ roomID: " " , // Id of the realtime subscription
55+ username: " " , // Nickname of the current user
56+ validate: false // Value that will change the display (false => Pseudo input; true => Message input)
5757 };
5858 },
59- /* snippet:end */
59+ /* snippet:end */
6060 methods: {
61- /* snippet:start:5 */
61+ /* snippet:start:5 */
6262 // This function return the right formated date depending on the timestamp
6363 getDate (timestamp ) {
6464 const date = new Date (timestamp);
65- return date .toString ().split (" GMT" )[0 ];
65+ return date .toLocaleString ().split (" GMT" )[0 ];
6666 },
67- /* snippet:end */
68- /* snippet:start:6 */
67+ /* snippet:end */
68+ /* snippet:start:6 */
6969 // This function will create a message object containing the informations we need to display it
70- getMessage (hit ) {
70+ getMessage (document ) {
7171 const message = {
7272 // The unique id of the document containing the message
73- _id: hit ._id ,
73+ _id: document ._id ,
7474 // The text of the message
75- value: hit ._source .value ,
75+ value: document ._source .value ,
7676 // The creation date
77- createdAt: hit ._source ._kuzzle_info .createdAt ,
77+ createdAt: document ._source ._kuzzle_info .createdAt ,
7878 // The author name
79- username: hit ._source .username
79+ username: document ._source .username
8080 };
8181 return message;
8282 },
83- /* snippet:end */
84- /* snippet:start:10 */
83+ /* snippet:end */
84+ /* snippet:start:10 */
8585 async sendMessage () {
8686 if (this .message === " " ) return ;
87- // Call the create method of the document controller
88- await kuzzle .document .create (" chat" , " messages" ,
87+ // Call the create method of the document controller
88+ await kuzzle .document .create (
89+ " chat" ,
90+ " messages" ,
8991 // Give as parameter the object that will be store in kuzzle
9092 {
9193 value: this .message ,
9294 username: this .username
93- });
95+ }
96+ );
9497 // Clear the user input
9598 this .message = " " ;
9699 },
97- /* snippet:end */
98- /* snippet:start:11 */
99- async subscribe_messages () {
100- // Call the subscribe method of the realtime controller and receive the roomId
101- // Save the id of our subscription (we could need it to unsubscribe)
102- this .roomID = await kuzzle .realtime .subscribe (
103- " chat" , // Id of the index
104- " messages" , // Id of the collection
105- {}, // Options
106- // Callback for notifications receive
107- notification => {
108- // Check if the notification interest us (only document creation)
109- if (
110- notification .type === " document" &&
111- notification .action === " create"
112- ) {
100+ /* snippet:end */
101+ /* snippet:start:11 */
102+ async subscribe_messages () {
103+ // Call the subscribe method of the realtime controller and receive the roomId
104+ // Save the id of our subscription (we could need it to unsubscribe)
105+ this .roomID = await kuzzle .realtime .subscribe (
106+ " chat" , // Id of the index
107+ " messages" , // Id of the collection
108+ {}, // Filter
109+ // Callback for notifications receive
110+ notification => {
111+ // Check if the notification interest us (only document creation)
112+ if (notification .type !== " document" ) return ;
113+ if (notification .action !== " create" ) return ;
113114 // Add the new message to our array
114115 this .messages = [
115116 this .getMessage (notification .result ),
116117 ... this .messages
117118 ];
118119 }
119- } );
120+ );
120121 },
121122 /* snippet:end */
122123 /* snippet:start:7 */
0 commit comments