简体   繁体   中英

OpenUI5: JS views vs. XML views

We're currently evaluating SAP's OpenUI5 in the context of a new project.

While most examples and tutorials tend to construct views using XML, out of personal preference I'd choose Javascript for the task. However I haven't poked around in OpenUI5 long enough to identify any specific strengths for either approach.

Are there any disadvantages in using JS? Or advantages even?

Many Thanks!

Update:

I agree with SDD64's answer. After using both approaches in parallel for a while, I can add the following pros and cons to the list:

XML advantages:

  • They help sticking to mvc since you're less tempted to introduce controller logic into views
  • potentially allow use of wysiwyg editors
  • quite implicit, hence easier to learn

XML disadvantages:

  • bad readability
  • modifying existing views is trickier at times
  • quite implicit, bad if you care about what is happening behind the scenes

I am also a fan of JS based views. Possible (dis)Advantages are in my eyes:

Advantages for JS

  • Object oriented creating of the view. You can create objects, arrange them, call methods on them and use any JavaScript function you want (eg loops)
  • For my taste, SAPUI5 XML based view seem to be a little too implicit. In JS, I can cleary read out of the coding what I am doing.
  • Pages can be dynamic
  • You stay in JavaScript all the time

Disadvantages for JS

  • XML favours a strict seperation between view and logic. Your are forced to use external formatters for example. I think XML views might be cleaner.
  • SAP ships all Fiori apps using XML views. So, you are a little forced to have atleast the knowledge in how to read XML based views.
  • You might have less lines of code in XML

** Update 2015-06-18 **

With the current OpenUI5 1.28 supporting conditions in XML views and the recently previewed 1.30 supporting binding for dynamic loading of XML fragments , I felt the need to update my answer.

With those new features, XML views gained a "healthy" amount of flexibility. Making them, for me, more attractive when compared to JS based view.

It's all in the eyes of the developer. Some devs will prefer to write html5 like (xml) views, other will prefer the feeling in control with the js views. Small disadvantage of the xml views is the extra step to convert them to js code. They should be a bit (neglitable) slower.

You can build your pages dynamically with XML-Views:

Controller

sap.ui.controller("my.own.controller", {
  onInit: function () {
    var jsonData = {
        simpsons: [
            {name: "Marge", gender: "female"},
            {name: "Homer", gender: "male"},
            {name: "Bart", gender: "male"},
            {name: "Lisa", gender: "female"},
            {name: "Maggi", gender: "female"},
            {name: "Snowball1", gender: "female"},
            {name: "Ruprecht", gender: "male"}
        ]
    };
    var oModel = new JSONModel(suggJSON);
    this.setModel(oModel, "myJsonModel");
  },
});

View :

 <core:View 
    xmlns:core="sap.ui.core" 
    xmlns:m="sap.m"
    xmlns="sap.ui.commons" 
    xmlns:html="http://www.w3.org/1999/xhtml"
    > 
      Content of Model:
      <m:VBox items="{myJsonModel>/simpsons}">
        <Button text="{myJsonModel>name} visible="{= ${myJsonModel>gender} === 'male'}"/>
        <Text text="{myJsonModel>name} visible="{= ${myJsonModel>gender} === 'female'}"/>
      </m:VBox>
  </core:View>

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