2014-10-02 119 views
1

我设置了淘汰赛的ViewModel这样当获得指标值:使用自定义映射淘汰赛

var Schedule = function(data, parent) { 
    var self = this; 
    ko.mapping.fromJS(data, {}, self); 
    self.foo = 'bar'; 
    ... 
}; 

var ScheduleViewModelMapping = { 
    'schedules': { 
     create: function(options) { 
      return new Schedule(options.data, options.parent); 
     } 
    } 
}; 

ko.mapping.fromJS(data, ScheduleViewModelMapping, self); 

$(document).ready(function() { 
    var ViewModel = new ScheduleViewModel; 
    ko.applyBindings(ViewModel); 
}); 

时间表是一个数组,我想能够获取某处当前计划的指标所以我可以将它传递给Schedule对象以及options.data和options.parent。

回答

3

我建议改变你的映射处理程序,以便它可以存储一些状态:

var ScheduleViewModelMapping = new (function(){ 
    var counter = 0; 

    this.schedules = { 
     create: function(options) { 
      //use counter as index as required 
      var s = new Schedule(options.data, options.parent); 
      counter++; 
      return s; 
     } 
    } 

    this.resetCounter = function() { 
     counter = 0; 
    } 
})(); 


ScheduleViewModelMapping.resetCounter(); 
ko.mapping.fromJS(data, ScheduleViewModelMapping, self);