How to update a property from a function in Aurelia

I want to update the value of a property in a push action, but I do not know how to access a property from a function!

export class Datas { prop1 = "my val"; } var connection = new WebSocket('ws://localhost:8787', 'json'); connection.onmessage = function (e) { // prop1 of Datas = e.data; }; 

Any ideas?

Edit: After loading the page, I want to update the data when I receive a push message.

Edit 2: test code

data.js:

 export class Data { static information = ''; } 

viewModel.js

 import {bindable} from 'aurelia-framework'; import { Data } from 'data'; export class ViewModel { constructor() { this.informaton = Data.information; } logState(){ console.log("state of Data.information : " + Data.information); console.log("state of this.information : " + this.information); } } var connection = new WebSocket('ws://localhost:8787', 'json'); connection.onmessage = function (e) { Data.information = e.data; console.log("receiving a message : Data.information = " + Data.information); }; 

viewModel.html:

 <template> <span id="spanAff">${information}</span> <br/> <input type="button" value="log" click.trigger="logState()" /> </template> 

logs:

"receiving the message: Data.information = 4"
"data status. information: 4"
"state of this information: undefined"

+5
source share
1 answer

I got this solution in another forum:

 export class ViewModel { constructor() { this.information = 'my val'; var connection = new WebSocket('ws://localhost:8787', 'json'); connection.onmessage = e => { this.information = e.data; }; } } 
+7
source

All Articles