Adam Harkus
Adam Harkus

Reputation: 2210

Getting and setting the default Model once another has been setup

My issue is that I have a default model specified in my manifest.json file as "".

However, I'm also setting up a view model in my controller.

var oModel = new JSONModel({
            questions: [{
                order: 1,
                title: "",
                criteria: "",
            }]
        });


        this.getView().setModel(oModel);

How do I restore set the view model back to the default model?

Is it simply a case of

this.getView().setModel("");

I'm asking because the default model is the service that contains all my read/writes.

Upvotes: 0

Views: 1724

Answers (2)

techippo.com
techippo.com

Reputation: 255

You should set named models after setting the default model.So your code should be changed like

var oModel = new JSONModel({
        questions: [{
            order: 1,
            title: "",
            criteria: "",
        }]
    });


    this.getView().setModel(oModel,"newModelName");

Now you can get default model by

this.getView().getModel()

and named json model by

this.getView().getModel("newModelName");

Upvotes: 1

Adam Harkus
Adam Harkus

Reputation: 2210

The solution was in the way the additional model was initialised and accessed, but naming it and referring to it as 'viewmodel'

This way, the default model can still be setup using

    oModel = this.getView().getModel();

In the Controller:

    this.getView().setModel(oModel,"viewmodel");
    var oContext = oModel.createBindingContext("/questions/0/");
    this.getView().setBindingContext(oContext,"viewmodel"); 

In the view :

    title="Question {viewmodel>order}"

Upvotes: 0

Related Questions