How to get another context path in factory method?

时间:2017-04-21 07:33:51

标签: javascript sapui5

Let's imagine that we have sap.m.UploadCollection and we bind the data to this collection which is done like this:

bind: function () {
    this._oUploadCollection.bindAggregation("items", {
        path: "/attachments",
        factory: jQuery.proxy(this._bindUploadCollectionItem, this)
    });
},

The example of the binding data is here:

{
    "attachments": [
        {
            "size": 123,
            "filename": "pdf.pdf",
            "id": "pdfId"
        },
        {
            "size": 440,
            "filename": "text.txt",
            "id": "textId"
        }
    ],
    "source":"personWhoAddedAttachments"
}

So, in _bindUploadCollectionItem I successfully can get size, filename and id by oContext.getProperty("nameOfParameter"), but cannot get source:

_bindUploadCollectionItem: function (sID, oContext) {
    return new sap.m.UploadCollectionItem({
        "id": oContext.getProperty("id"),               
        "fileName": oContext.getProperty("filename"),                              
        "attributes": [                                          
        {
             "title": "author",
             "text": oContext.getProperty("../source") // <- problem
        }]
   });
},

So, because I bind attachments it is kind of clear that I could not get source, but how to reach it if I need it?

1 个答案:

答案 0 :(得分:0)

这取决于您想要获得的模型的属性。如果它真的像你描述它并且目标属性在/source绝对模型路径中,那么在工厂函数中获取它的最简单方法是使用:oContext.getModel().getProperty("/source")

如果你需要一些集合内部的东西(并且某种程度上取决于当前的上下文),你可以实现类似于你尝试使用某些东西的..路径构造的效果:

var sPath = oContext.getPath(),
    sParent = sPath.substring(0, sPath.lastIndexOf("/")),
    sText = oContext.getModel().getProperty(sParent + "/source");
return new sap.m.UploadCollectionItem({
    "id": oContext.getProperty("id"),               
    "fileName": oContext.getProperty("filename"),                              
    "attributes": [{
         "title": "author",
         "text": sText
    }]
});

您基本上通过搜索路径中的最后/来获取父对象路径。您可以重复应用(或使用拆分,弹出一些元素,然后连接)来到祖先(例如父母的父母)。

相关问题