cancel
Showing results for 
Search instead for 
Did you mean: 

Change model in XML-template.

Former Member
0 Kudos

Hi!

Please help understand the problem!

I learned XML templates. Following the example of 'XML Templating in SAPUI5', I create a view and places it in the main view.

Main.controller.js


//...

var o_goals_data_model = new JSONModel(this.goals_data);

var o_settings_model = new JSONModel(this.settings);

var o_form_view_template = sap.ui.view({

  preprocessors: {

  xml: {

  models: {

  goals_data: o_goals_data_model,

  settings: o_settings_model

  }

  }

  },

  type: sap.ui.core.mvc.ViewType.XML,

  viewName: 'sap.app.GoalSet.view.Form'

});

this.getView().byId('goalset_main_shell').addContent(o_form_view_template);

//...

However, the controller can not I turn to the model in view. If I need to change the model, how it can be done?

Form.controller.js


ap.ui.define( ["sap/ui/core/mvc/Controller"],

  function (Controller, History, JSONModel) {

  "use strict";

  return Controller.extend("sap.app.GoalSet.controller.Form", {

  onInit : function () {},

  onTogleEditTables: function(){

  var o_view = this.getView();                                    //OK

  var o_model = o_view.getModel("settings");           //undefined

  var status = o_model.getProperty("/edit_tables");   //Error!

  //o_model.setProperty("/edit_tables", !status);        //???

  sap.m.MessageToast.show(status);

  },

  });

});

Sorry for my English ... I hope my problem is clear.

Accepted Solutions (1)

Accepted Solutions (1)

saivellanki
Active Contributor
0 Kudos

Hi Sergey,

Try like this -


onTogleEditTables: function(){

var o_view = this.getView();                       //Get View

var o_Models = o_view.mPreprocessors.xml.models;      //Get models defined in preprocessor

var o_model = o_Models.settings;                //Get Settings Model    

var status = o_model.getProperty("/edit_tables");  

sap.m.MessageToast.show(status);

},

Regards,

Sai Vellanki.

Former Member
0 Kudos

Hello, Sai!

Thank you for your help! If not difficult to answer one more question ...

To the properties of the model "edit_tables" I tied the buttons properties: 'visible = "{settings> / edit_tables}"'.

But when I change the model (edit_talbes = false / true), nothing happens. The buttons are not hidden.

Form.controller.js


//...

onTogleEditTables: function(){

  var o_view = this.getView();                     //Get View

  var o_models = o_view.mPreprocessors.xml.models; //Get models defined in preprocessor

  var o_model_sett = o_models.settings;            //Get Settings Model  

  var status = o_model_sett.getProperty("/edit_tables");

  o_model_sett.setProperty("/edit_tables", !status);

},

//...

FooterToolbar.fragment.xml


<core:FragmentDefinition

    xmlns="sap.m"

    xmlns:core="sap.ui.core"

    xmlns:template="http://schemas.sap.com/sapui5/extension/sap.ui.core.template/1">

    <Toolbar>

    <Button

    text="Edit"

    visible="true"

    press="onTogleEditTables" />

 

    <ToolbarSpacer />

  

  <!-- ... -->

    <Button

    text="Save"

    visible="{settings>/edit_tables}"

    press="testMethod" />

    <Button

    text="Cancel"

    visible="{settings>/edit_tables}"

    press="testMethod" />

    </Toolbar>

  

</core:FragmentDefinition>

What should I do to changes were seen in view?

saivellanki
Active Contributor
0 Kudos

Hi Sergey,

Are you sure about the model property value. Were you able to get the boolean value, when you debug and see o_model_sett.getProperty("/edit_tables") ?


One of a similar kind sample, you can find it here: Plunker

But it is not based on MVC, it is just a plain HTML page having the model bound to editable property of the text field controls which are used in table. Click on Non-Editable entries in the table are non-editable. Click on Editable then the entries are editable.

Regards,

Sai Vellanki.

Answers (2)

Answers (2)

WouterLemaire
Active Contributor
0 Kudos

Hi,

Not sure what you're doing wrong, but here you have a good example:

http://help.sap.com/saphelp_uiaddon10/helpdata/en/5e/e619fc1370463ea674ee04b65ed83b/content.htm

Kind regards,

Wouter

karthikarjun
Active Contributor
0 Kudos

Dear Sergey,

Where you declare this model var o_model = o_view.getModel("settings");   .

Setting model?

Forex:

Var Json = new json(data):

This.getView().setModel(json,'settings'):

Did you wrote  this statement in your code?

Thanks,

Karthik A

Former Member
0 Kudos

Hello, dear Karthik.

"this.settings" - a structure.

When I wrote:  

"var o_settings_model = new JSONModel (this.settings);"

  It was created json-model with the name   'o_settings_model'.

If I use the   "this.getView (). SetModel (o_settings_model, 'settings');",  then I can not use  "<template: if / repeat / width ...>"   in my xml-template. But herein lies the chief meaning.

Maybe I'm wrong somewhere ...