0

I just started learning native-script framework and I'm following the "Get Started with JavaScript" tutorial from the official website. I have a background in Java so I'm more familiar with typescript. I tried to replace all JavaScript code to the typescript equivalent.

So far everything is OK, but when I reached Chapter three > 3.4: Adding a view model section , I got confused on how to implement the Observable in typescript. this is the code from the website:

var observableModule = require("data/observable");

var user = new observableModule.fromObject({
    email: "[email protected]",
    password: "password"
});

And this is what I came up with:

import {Observable} from 'data/observable';
class User extends Observable {
    email = "[email protected]";
    password = "password";
}

let user = new User();

When I tested it, it seems to work. Are the above codes equivalent or did I miss something?

1

1 Answer 1

3

Yes this is the way to make an "object" observable. A different but also similar approach would be to make your entire "ViewModel" observable for example:

import { Observable } from "data/observable";

export class ViewModel extends Observable {    
    constructor() {
        super();

        this.set("propertyA", 42);
        this.set("propertyB", "some value");
    }

    public updatedValues() {
        this.set("propertyA", 3.14);
        this.set("propertyB", "some new value");
    }
}

after that if you set that ViewModel to be bindingContext of the Page you can do your normal bindings and any updates to the those properties of the ViewModel will be propagated to the view.

<Label text="{{ propertyA }}"/>
<Label text="{{ propertyB }}"/>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Vladimir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.