Search code examples
javascriptsocket.ionativescript

How to push new items into lisview after socketio event is fired in nativescript


i created a socket.io server to use with my nativescript app. its working fine but the issue i'm having is when the "chat message" event is fired, i want to push the new message into the listview.

This is what i tried

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"

Please how do i fix this


Solution

  • I used this and it worked

    viewModel.dataItems.push(
                    {
                        sender: data.sender,
                        message: data.message,
                        date: data.date,
                        type: data.type
                      }
                    );