官术网_书友最值得收藏!

Listening to message saved events

Storing the message in the database takes some amount of time and we want to update the UI or reflect the state of the UI after the message is successfully saved into the database. We specify an EventSource and add an EventListener to receive the callback when a new message is stored in the database.

When a new message is saved into the database the callback function of the event listener is called and we then plot the message on the map using ol.Feature:

    /* Reactive: Event listener to get updates when new message is   
saved */

var source = new EventSource("/message/subscribe");

// Callback function called on event update
source.addEventListener('message', function (e) {
var message = $.parseJSON(e.data);
var feature = new ol.Feature({
geometry: new ol.geom.Point(message.location.coordinates),
content: message.content
});
vectorSource.addFeature(feature);
}, false);
主站蜘蛛池模板: 延长县| 闸北区| 千阳县| 屏东县| 永泰县| 东乡族自治县| 巴中市| 靖宇县| 延安市| 顺义区| 浦城县| 封丘县| 富宁县| 白沙| 车险| 称多县| 阳泉市| 南川市| 安龙县| 岗巴县| 常山县| 常山县| 包头市| 山东省| 金门县| 德保县| 仙居县| 五寨县| 永靖县| 文山县| 凤冈县| 北海市| 榆中县| 枣庄市| 如东县| 页游| 水富县| 苍梧县| 车险| 隆回县| 靖江市|