2013-02-25 75 views
2

我已经定义了一个剑术数据源与传输方法(创建,读取,更新和破坏),以及一模式定义:剑道MVVM绑定添加/插入项目,如何?

$(document).ready(function() { 
     var viewModel = kendo.observable({ 
      dsMedication: new kendo.data.DataSource({ 
       type: "json", 
       serverFiltering: true, 
       serverPaging: true, 
       pageSize: 6, 
       error: function(e) { 
        //alert(e.responseText); 
        //var error_obj = $.parseJSON(e.responseText); 
        //if (error_obj.Message != null) { 
        //alert(error_obj.Message); 
        //} 
       }, 
       transport: { 
        read: { 
         contentType: "application/json; charset=utf-8", 
         type: "POST", 
         url: "../Services/svcMedication.asmx/SearchMedication", 
         dataType: "json", 
         cache: false 
        }, 
        update: { 
         contentType: "application/json; charset=utf-8", 
         type: "POST", 
         url: "../Services/svcMedication.asmx/SaveMedication", 
         dataType: "json", 
         cache: false 
        }, 
        destroy: { 
         url: "../Services/svcMedication.asmx/DeleteMedication", 
         type: "DELETE", 
         dataType: "json", 
         cache: false 
        }, 
        create: { 
         contentType: "application/json; charset=utf-8", 
         type: "POST", 
         url: "../Services/svcMedication.asmx/SaveMedication", 
         cache: false 
        }, 
        parameterMap: function(options, operation) { 
         if (operation !== "read" && options.models) { 
          return kendo.stringify({ models: options.models }); 
         } 
         options.MedicationParam = $('#acMedications').val(); 
         return kendo.stringify(options); 
        } 
       }, 
       batch: true, 
       schema: { 
        data: "d", 
        model: { 
         id: "MedicationId", 
         fields: { 
          MedicationId: { 
           type: "number", 
           editable: false // this field is not editable 
          }, 
          Name: { 
           type: "text", 
           validation: { // validation rules 
            required: true // the field is required 
           } 
          } 
         } 
        } 
       } 
      }), 
      SelectedMedication: null, 
      HasChanges: false, 
      save: function() { 
       //if (this.SelectedMedication == null) { 
        //this.dsMedication.add({ MedicationId: this.get("MedicationId"), Name: this.get("Name") }); 
       //} 
       this.dsMedication.sync(); 
       this.set("HasChanges", false); 
      }, 

      remove: function() { 
       if (confirm("Are you sure you want to delete this record?")) { 
        this.dsMedication.remove(this.SelectedMedication); 
        this.set("SelectedMedication", this.dsMedication.view()[0]); 
        this.change(); 
       } 
      }, 
      showForm: function() { 
       return this.get("SelectedMedication") !== null; 
      }, 
      change: function() { 
       this.set("HasChanges", true); 
      } 
     }); 

     kendo.bind($("#fmMedication"), viewModel); 
    }); 

我的形式元素包含适当的数据绑定的属性,以形成元件,并且我打电话给kendo.bind传递表单。目标是表格用于编辑记录。

如果我搜索一个项目并对其进行更改,那么每件事都像一个魅力!

我的问题是:

我不知道如何编写代码来初始化形式增加新记录的数据源。

我一直在找不到任何地方的例子或资源!

任何帮助,将不胜感激!谢谢!

回答

1

基本上,因为你没有明确定义模型,你不能轻易地创建它的实例。因此,如果您已经像here那样分别声明模型 - 创建实例会更容易一些,并且很可能您会知道接下来要做什么。

在你的情况,你可以这样创建模型的实例:

var newRecord = new viewModel.dsMedication.reader.model(); 

一旦你有了新的记录,你可以把它或者通过添加或插入方法很容易地添加到数据源(它们都描述在文档中)。

最后,当你有本地添加的记录(你应该能够立即看到屏幕上的变化),你可以调用同步方法的dataSource发送这些新的记录被保存在服务器上 - 创建传输选项将被使用。

+0

在我的问题datasource hasbeen更新,但值不去创建操作或网格self.dsProduct.add(vm_Currencies.selectedRow);我已经声明模型seperatly,也做了视图模型的功能我应该怎么做创建操作被称为 – 2015-06-26 09:34:26

+2

你指定了datasource.schema.model配置中的model.id字段? – 2015-06-26 21:27:57

+0

我必须在添加到我的数据源之前将数据上的model.id设置为零,然后才能实际调用create方法。 – ooXei1sh 2017-08-22 17:45:08