2

我有一个非常大的模型,需要与我的视图绑定。在 UI 上,我需要提供添加/编辑功能(使用 jquery 模型弹出窗口)。我正在使用映射插件从服务器映射我的 json 数据。这就是事情变得棘手的地方..所以我想要实现的是它是否有可能以某种方式创建一个对象 -

var obj = ko.mapping.toJS(viewmodel)

我正在使用这样的自定义绑定 -

<div id="dailog" title="Employee .." class="dialog" data-bind="Dialog: { autoOpen: false, resizable: false, modal: true, height: 'auto', width: 'auto', position: {my: 'center', at: 'center'} }, template: { name: 'editTmpl', data: DataService.selectedItem, if: DataService.selectedItem }, openDialog: DataService.selectedItem"> 
</div>
var mapping = {
    'EmpList': {
    create: function (options) {
    return new EmpMapping(options.data);
    }
};

var EmpMapping = function (data) {
      ko.mapping.fromJS(data, {}, this);
        };

  var DataService = (function () {
  var mapping,
  viewModel, 
  selectedItem = ko.observable("");

  function editItem(context) { //this is working.
    selectedItem(context);
  }

  function AddNewItem(obj) {  // this is not working .. reason being I am not able to create new instance of a Employee. I was trying with ko.mapping.toJS 
       debugger
       var myObj = ko.toJS(viewModel.EmpList);
       var z = ko.mapping.fromJS({}, mapping, myObj);
       selectedItem(z);
   }

  return{
      editItem: editItem,
      saveItem: saveItem,
      init : init
      selectedItem : selectedItem
  }
}());
4

0 回答 0