SAP CRM Fiori应用My Note的OData调用设计
Sent: Monday, December 22, 2014 4:14 PM
这里简单把现有的UI逻辑总结一下:
Notes Tab UI:
<IconTabFilter id="tab_notes" icon="sap-icon://notes"
key="Notes" text="{i18n>NOTES}" iconColor="Neutral">
<FeedInput id="notesList" textMaxLength="1000" text="{json>Content}"
post="_handleAddNote" showIcon="true" icon="{json>icon}" maxLines="3">
</FeedInput>
<List id="listItem" showSeparators="Inner" growing="true" growingThreshold="4" growingScrollToLoad="false"
items="{json>/OpportunityNotesSet}">
<FeedListItem sender="{path : 'json>Creator'}" senderActive="false"
timestamp="{path:'json>CreatedAt' , formatter: 'cus.crm.opportunity.util.Formatter.notesDateFormatter'}"
text="{path : 'json>Content'}" />
</List>
</IconTabFilter>
标签表示Tab Bar上的一个Tab, 表示新输入Note的控件, 表示每一条需要显示的Note。
显示的每一条Note是绑定在listItem model的OpportunityNoteSet属性上,每次点击Note Tab的时候,会执行S3.controller.js中的notesTabSelected()方法:
notesTabSelected : function(){
var oModel = this.getView().getModel();
this.byId("listItem").setNoDataText(sap.ca.scfld.md.app.Application.getImpl().getResourceBundle().getText('LOADING_TEXT'));
this.byId('listItem').getModel('json').oData.OpportunityNotesSet = [];
this.byId('listItem').getModel('json').updateBindings();
oModel.read(this.sPath, null, [ "$expand=Notes" ], true,
// 在这里发起OData的调用,http://localhost:8080/sap/opu/odata/sap/CRM_OPPORTUNITY/Opportunities(guid'0090FA0D-8D72-1ED3-98C7-DC2E25228BC4')?$expand=Notes
jQuery.proxy(function(odata, response) {
var tab = this.getView().byId("listItem");
var oJSONModel = tab.getModel("json");
var oData = oJSONModel.oData;
// 返回的结果绑定到listItem model的OpportunityNotesSet上
oData.OpportunityNotesSet = response.data.Notes.results;
if(oData.OpportunityNotesSet.length == 0){ this.byId("listItem").setNoDataText(sap.ca.scfld.md.app.Application.getImpl().getResourceBundle().getText('NONOTES'));
}
// 用新返回的数据刷新UI
oJSONModel.updateBindings();
},this),
jQuery.proxy(function(oError){
this.handleErrors(oError);
},this)
);
},
每次添加一条Note的时候会调用FeedInput控件上绑定的post事件对应的_handleAddNote()方法:
<FeedInput id="notesList" textMaxLength="1000" text="{json>Content}"
post="_handleAddNote" showIcon="true" icon="{json>icon}" maxLines="3">
</FeedInput>
_handleAddNote : function(oEvent) {
// 获取输入的内容
var sText = oEvent.getParameter("value");
if (sText) {
var that = this;
var oModel = this.getView().getModel();
var headerGuid = this.byId('info').getModel('json').getData().Guid;
var oEntry = {
HeaderGuid : headerGuid,
Content : sText
};
// 创建一条新的Note http://localhost:8080/sap/opu/odata/sap/CRM_OPPORTUNITY/OpportunityNotesSet
oModel.create('/OpportunityNotesSet',
oEntry,
null,
jQuery.proxy(function() {
var that = this;
// 创建新的Note成功之后的回调方法里面重新获取新的Note List, 然后绑定到model的OpportunityNotesSet上
oModel.read( that.sPath, null, [ "$expand=Notes" ],true,
function(odata,response){
that.byId('listItem').setModel(new sap.ui.model.json.JSONModel({OpportunityNotesSet: odata.Notes.results}),"json");
});
}, this),
function(oMessage) {
that.displayResponseErrorMessage(oMessage,
sap.ca.scfld.md.app.Application.getImpl().getResourceBundle().getText('SAVE_FAILED'));
}
);
}
},
要获取更多Jerry的原创文章,请关注公众号"汪子熙":