有人能告诉我我的代码出了什么问题吗?我已经成功地在标准js文件(SearchResult.view.js)上添加了自定义字段。我知道这不是添加自定义字段的最佳实践。因此,我实现了一个用于添加自定义字段的pre post方法。
不幸的是,当我将我的自定义代码块移动到pre post方法时,它不是添加1行(字段),而是添加了多行。我试着创建一个计数器,但它也不起作用
下面是我的自定义js代码。提前感谢!
function ADDCUSTOMFIELD1(){
};
ADDCUSTOMFIELD1.prototype.CUSTOM_POST_EXIT = function(methodName,view,controller, methodSignaure) {
if (!sap.ui.getCore().byId("ni_home"))
return;
else add_custom_item();
};
function add_custom_item(){
if (sap.ui.getCore().byId("subMatrix")){
// Supplier Name
matrixSubRow = new sap.ui.commons.layout.MatrixLayoutRow();
control = new sap.ui.commons.Label({
text : Appcc.getText("SUPPLIER_TEXT") + ":"
});// control.addStyleClass("search_middle_spacing");
matrixCell = new sap.ui.commons.layout.MatrixLayoutCell();
matrixCell.addContent(control);
control = new sap.ui.commons.Label();
control.bindProperty("text", "vendor_name");
if (sap.ui.getCore().getConfiguration().getRTL()) {
control.addStyleClass("search_middle_spacingNewRTL");
Appcc.addStyleClass(control, 'search_middle_spacingNew', true);
} else
control.addStyleClass("search_middle_spacingNew");
matrixCell.addContent(control);
// control = new sap.ui.commons.Label();
// control.bindProperty("text", "itm_price");
// control.addStyleClass("search_middle_spacing");
// matrixCell.addContent(control);
matrixSubRow.addCell(matrixCell);
sap.ui.getCore().byId("subMatrix").addRow(matrixSubRow);
}
}
发布于 2016-01-07 23:04:02
您的自定义代码块会添加多行,因为视图上的每个事件都会调用CUSTOM_POST_EXIT函数。单个视图上的多个事件被激发(beforerender、render、ondatamodelloaded等)。methodName参数是事件的名称。尝尝这个
function ADDCUSTOMFIELD1() {};
ADDCUSTOMFIELD1.prototype.CUSTOM_POST_EXIT = function(methodName, view, controller, methodSignaure) {
var viewId = controller && controller.getView().getId();
console.log(viewId, methodName)
if (viewId === 'name_of_your_view' && methodName === 'onDataModelLoaded')
//implement your customization
}
}您应该会看到,页面上的每个视图都会多次调用此函数。
因此,您应该检查调用CUSTOM_POST_EXIT方法的视图和eventName,并仅在一个视图/事件组合中实现您的自定义。
https://stackoverflow.com/questions/33895488
复制相似问题