2015-05-05 11 views
5

Desidero aggiornare il valore di una proprietà su azione push, ma non so come accedere alla proprietà da una funzione!Come aggiornare la proprietà dalla funzione in Aurelia

export class Datas { 
    prop1 = "my val"; 
} 

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

Qualche idea?

Modifica: Dopo il caricamento della pagina, desidero aggiornare i dati quando si riceve un messaggio push.

Modifica 2: codice di test

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> 

tronchi:

"ricezione di un messaggio: Data.information = 4"
"stato di Data.information: 4"
"stato di this.information: non definito"

risposta

7

ho ottenuto questa soluzione su un altro forum:

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

ah sì, è così semplice, non è vero? :) Ho aggiornato la tua soluzione. Non ti serviva l'importazione associabile. Inoltre, non hai bisogno della variabile myModel se usi le funzioni freccia. Provaci. –

+0

grazie per le correzioni. Sì, è davvero semplice! – Seb

Problemi correlati