Aurelia computed using an object

I have an object statethat has a property session, this property can be either objector null.

I don't want to check getter dirty isSessionActive(), so I would like to use computedFrom(). However, it computerFrom()does not work when this object changes if it was not previously undefined.

Can I do this without a special isSessionActiveboolean property in my state store?

@autoinject
export class Home {
    firstName: string = "user";
    private state: State;

    constructor(private store: Store) {
       store.state.subscribe(
            response => this.state = response
       )
    }

    @computedFrom('state.activeSession')
    get isSessionActive() {
        return this.state.activeSession !== null;
    }
}
+4
source share
2 answers

In the end, I just did the following:

isSessionActive: boolean = false;

constructor(private store: Store) {
    store.state.subscribe(
        response => { 
            this.state = response;
            this.isSessionActive = response.activeSession !== null;
        }
    )
}
+2
source

, , , . , . , , .

, , .


:

private state: State;

// ...

@computedFrom('state.activeSession')

, Aurelia , , Aurelia , . , , ( ), (.. , ), .

, , , , - , , -, - , . , :

private state: State;

TypeScript, state . , , JavaScript - state TypeScript.

, :

class A {
  x: number;
}

JavaScript :

function A() {
}

, this.x! , :

class B {
  x: number = undefined; // or null, or whatever you like
}

JavaScript :

function B() {
  this.x = undefined;
}

, . - Aurelia , , ( JavaScript) . , :

var a = new A();
var b = new B();
console.log(a.x); // undefined
console.log(b.x); // undefined

a.x b.x undefined. : a.x is undefined, a, b.x - undefined, undefined - , undefined. Aurelia , .

, , , , (, undefined, null) , . .

+1

Source: https://habr.com/ru/post/1683596/


All Articles