2015-09-02 26 views
0

我的模型使用OData的模型

var oModel = new sap.ui.model.odata.ODataModel("../services/myexp.xsodata", false); 
sap.ui.getCore().setModel(oModel,'data'); 

現在我想新的記錄添加到這個模型中添加新的記錄。簡單的代碼 -

openUserCreateDialog: function(){ 
    var oUserCreateDialog = new sap.ui.commons.Dialog(); 
    var oSimpleForm1 = new sap.ui.layout.form.SimpleForm({ 
     maxContainerCols: 2, 
     content:[ 
      new sap.ui.core.Title({text:"Create"}), 
      new sap.ui.commons.Label({text:"User"}), 
      new sap.ui.commons.TextField({value:""}), 
      new sap.ui.commons.Label({text:"Date"}), 
      new sap.ui.commons.TextField({value:""}), 
      new sap.ui.commons.Label({text:"Description"}), 
      new sap.ui.commons.TextField({value:""}) 
     ] 
    });    
    oUserCreateDialog.addContent(oSimpleForm1); 
    oUserCreateDialog.addButton(
     new sap.ui.commons.Button({ 
      text: "Submit", 
      press: function() { 
       var content = oSimpleForm1.getContent(); 
       var oEntry = {}; 
       oEntry.User = content[2].getValue(); 
       oEntry.Date = content[4].getValue(); 
       oEntry.Description = content[6].getValue(); 

       sap.ui.getCore().getModel().create('data>/user', oEntry, null, function(){ 
         oUserCreateDialog.close(); 
         sap.ui.getCore().getModel().refresh(); 
        },function(){ 
         oUserCreateDialog.close(); 
         alert("Create failed"); 
        } 
       ); 
      } 
     }) 
    ); 
    oUserCreateDialog.open(); 
}, 

當我提交表單,它拋出一個錯誤原樣

Uncaught TypeError: sap.ui.getCore(...).getModel(...).create is not a function 

什麼是錯我的代碼。請幫忙。謝謝

回答

1

您的代碼的問題是您將模型設置爲命名模型,但訪問默認模型。

sap.ui.getCore().setModel(oModel,'data'); 

上述代碼設置oModel名稱爲「數據」,但在閱讀您正在訪問的默認模型

sap.ui.getCore().getModel().create('data>/user', oEntry, null, function(){ 
         oUserCreateDialog.close(); 
         sap.ui.getCore().getModel().refresh(); 
        },function(){ 
         oUserCreateDialog.close(); 
         alert("Create failed"); 
        } 
       ); 

因此,雖然訪問模型調用命名模式

sap.ui.getCore().getModel('data').create('/user', oEntry, null, function(){ 
        oUserCreateDialog.close(); 
        sap.ui.getCore('data').getModel().refresh(); 
       },function(){ 
        oUserCreateDialog.close(); 
        alert("Create failed"); 
       } 
      ); 

這將起作用。

+0

謝謝...它的工作 –