是否可以从ko.mapping.toJS生成对象以构建添加/编辑屏幕

时间:2013-11-09 06:19:35

标签: knockout.js knockout-mapping-plugin

我有一个非常庞大的模型,我需要与我的视图绑定。在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
  }
}());

0 个答案:

没有答案
相关问题