简体   繁体   中英

Setting and getting model values in OpenUI5

I am trying to play with OpenUi5. I want to set 'artificial model' change the value and print it. My code:

onInit: function () {
    this.getView().setModel(new sap.ui.model.json.JSONModel());
    this.getView().getModel().setData({"name":"Jon"});
    this.getView().getModel().setProperty("name", "Ann");

    var name = this.getView().getModel().getProperty("name");
    window.alert(name);

It says that name is null . Why is that so?

you might want to look into this tutorial from SAP: https://sapui5.hana.ondemand.com/1.54.8/#/topic/e5310932a71f42daa41f3a6143efca9c

but for a quick answer: you are missing a / , and you dont need the " in you json

this.getView().getModel().setData({name:"Jon"});
...
var name = this.getView().getModel().getProperty("/name");

obv same for the set property line

also for easier readability of you code i'd do smth in the lines of:

onInit: function () {
    var oYourModel = new JSONModel({
            name: "Jon"
    });
    this.getView().setModel(oYourModel, "modelName");
    this.getView().getModel("modelName").setProperty("/name", "Ann");

    var name = this.getView().getModel().getProperty("/name");
    window.alert(name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM