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

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);
主站蜘蛛池模板: 高碑店市| 哈巴河县| 丰原市| 南和县| 卢龙县| 武邑县| 岑溪市| 海阳市| 赞皇县| 阿城市| 陇南市| 宁明县| 合肥市| 襄汾县| 镇巴县| 龙门县| 孝义市| 米林县| 嘉兴市| 兴隆县| 蓬莱市| 岑巩县| 临泽县| 桦川县| 布拖县| 渭源县| 井陉县| 西乌珠穆沁旗| 霍城县| 丹阳市| 怀宁县| 海南省| 昭苏县| 昌都县| 大同县| 汉沽区| 格尔木市| 紫阳县| 曲松县| 岑溪市| 故城县|