How to pass control reference to formatter in XMLView?

前端 未结 3 1943
执念已碎
执念已碎 2020-12-17 07:25

In SAPUI5\'s JSView, it is quite easy to pass the current control reference to a formatter function:

oTable.bindItems(         


        
3条回答
  •  无人及你
    2020-12-17 08:08

    The answer of @codeworrior in this issue make it more clear:

    A name that starts with a dot (e.g. ".foo") is searched for in the controller of the view and the execution context will be enforced to being the controller.

    All other names are resolved starting from the window object and they get the control/element as context which holds the binding.


    Just complement @hirse's answer, and for those who get formatter function xxx not found error:

    both .formatter.myformatter and mynamespace.Formatter.myformatter is working.

    The logic of parse formatter is in sap.ui.base.BindingParser.resolveRef(oBindingInfo,'formatter')

    BindingParser seems different in sapUI5(1.54) and openUI5. I'll take sapUI5 version as an example.

    If formatter name starts with a dot ('.'), eg. .formatter.myformatter, lookup will start with the given context(the Controller of the view), otherwise("mynamespace.Formatter.myformatter") it will start with the global context (window).

    and jQuery.sap.getObject("formatter.myformatter", oContext) or jQuery.sap.getObject("mynamespace.Formatter.myformatter", window) is called.

    so If you get formatter function xxx not found! error. set a break point in jQuery.sap.getObject, and check if there is "myformatter" in oContext or window object.

    And I found that there is no mynamespace.Formatter.myformatter in my window object. so I change my formatter from

    sap.ui.define([], function() {
      return {
        myformatter: function () {}
      }
    })
    

    To

    sap.ui.define([], function() {
      var Formatter = {
        myformatter: function () {}
      }
      
      return Formatter
    }, /* bExport */ true)
    

    And it's working.

提交回复
热议问题