Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update property from function in Aurelia

I want to refresh a property's value on push action, but i don't know how to access to the 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 the page loads, I want to refresh the data when receiving a push message.

Edit 2 : testing 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 a message : Data.information = 4"
"state of Data.information : 4"
"state of this.information : undefined"

like image 580
Seb Avatar asked May 05 '15 15:05

Seb


1 Answers

I got this solution on 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;
    };
  }
}
like image 120
Seb Avatar answered Oct 05 '22 07:10

Seb