如何在 nativescript 中触发 socketio 事件后将新项目推送到 lisview
How to push new items into lisview after socketio event is fired in nativescript
我创建了一个 socket.io 服务器来与我的 nativescript 应用程序一起使用。它工作正常,但我遇到的问题是当“聊天消息”事件被触发时,我想将新消息推送到列表视图中。
这是我试过的
dataItems: new ObservableArray(),
onLoadMoreItemsRequested: function (args) {
console.log("---load more item---");
const that = new WeakRef(this);
const listView = args.object;
if (this._sourceDataItems.length > 0) {
setTimeout(function () {
that.get().addMoreItemsFromSource(25);
listView.notifyLoadOnDemandFinished();
socketIO.on('chat message', function (data) {
//console.dir(msg);
var toast = Toast.makeText("New Message");
toast.show();
this.dataItems.push(
{
sender: data.sender,
message: data.message,
date: data.date,
type: data.type
}
);
});
}, 1500);
args.returnValue = true;
}
},
The error im getting is, "cannot read property push of undefined"
请问我该如何解决这个问题
我用过这个并且有效
viewModel.dataItems.push(
{
sender: data.sender,
message: data.message,
date: data.date,
type: data.type
}
);
我创建了一个 socket.io 服务器来与我的 nativescript 应用程序一起使用。它工作正常,但我遇到的问题是当“聊天消息”事件被触发时,我想将新消息推送到列表视图中。
这是我试过的
dataItems: new ObservableArray(),
onLoadMoreItemsRequested: function (args) {
console.log("---load more item---");
const that = new WeakRef(this);
const listView = args.object;
if (this._sourceDataItems.length > 0) {
setTimeout(function () {
that.get().addMoreItemsFromSource(25);
listView.notifyLoadOnDemandFinished();
socketIO.on('chat message', function (data) {
//console.dir(msg);
var toast = Toast.makeText("New Message");
toast.show();
this.dataItems.push(
{
sender: data.sender,
message: data.message,
date: data.date,
type: data.type
}
);
});
}, 1500);
args.returnValue = true;
}
},
The error im getting is, "cannot read property push of undefined"
请问我该如何解决这个问题
我用过这个并且有效
viewModel.dataItems.push(
{
sender: data.sender,
message: data.message,
date: data.date,
type: data.type
}
);