我对knockoutjs很陌生,需要一些关于像viewmodel这样的父子问题的帮助。
var DailyItems = function (data) {
var p = this;
this.Day = ko.observable(data.Day);
this.Date = ko.observable(data.Date);
this.Required = ko.observable(data.Required);
this.RequiredActive = ko.observable(true);
this.SetupTime = ko.observable(data.SetupTime);
this.CloseTime = ko.observable(data.CloseTime);
this.MinHrsPerDay = ko.observable(data.MinHrsPerDay);
this.MaxHrsPerDay = ko.observable(data.MaxHrsPerDay);
this.MinWorkShift = ko.observable(data.MinWorkShift);
this.WorkSegments = ko.observableArray([]);
var records = $.map(data.WorkSegments, function (x) { return new WorkShift(p, x) });
this.WorkSegments(records);
this.EnableAdd = ko.observable(ko.toJS(this.WorkSegments).length < 8);
this.Add = function () {
this.WorkSegments.push({
Parent: p,
ID: "",
Day: data.Date,
Location: UNIT_ID,
Role: "",
EmployeeRoles2: "[]",
ShiftStart: "",
ShiftEnd: "",
LocationActive: true,
RoleActive: true
});
this.EnableAdd(ko.toJS(this.WorkSegments).length < 8);
}
this.Delete = function (item) {
this.WorkSegments.remove(item);
this.EnableAdd(ko.toJS(this.WorkSegments).length < 8);
}
};
子模型如下:
var WorkShift = function (parent, data) {
var self = this;
this.Parent = ko.observable(parent);
this.ID = ko.observable(data.ID);
this.Day = ko.observable(data.Day);
this.Location = ko.observable(data.Location);
this.Parent = ko.observable(0);
this.LocationActive = ko.observable(true);
this.RoleActive = ko.observable(true);
this.ShiftStart = ko.observable(data.ShiftStart);
this.ShiftEnd = ko.observable(data.ShiftEnd);
this.EmployeeRoles2 = ko.observableArray([{ "ID": 0, "Name": "Volume"}]);
this.Location.subscribe(function (branchId) {
$.ajax({
type: "POST",
url: SERVER_PATH + '/WebServices/AttributeService.asmx/GetDataOnLocationChange',
data: "{" + "clientId: '" + CLIENT_ID
+ "', unitId: '" + branchId
+ "', effectiveDate:'" + EFFECTIVE_DATE
+ "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
var d = JSON.parse(res.d);
self.EmployeeRoles2(d.Roles);
if (d.IsSection == true) {
self.RoleActive(false);
self.Parent(d.Parent);
}
else if (d.IsRegularBranch == false) {
self.RoleActive(false);
self.LocationActive(false);
}
else {
self.RoleActive(true);
self.LocationActive(true);
}
var tasks = self.Parent().WorkSegments();
//Requirement: for any day of the week, if there is more than one work segment
//at different branches the required type should be set to 'On' and made disable
if (tasks.length > 1) {
ko.utils.arrayForEach(tasks, function (i) {
if ((d.isSection == false && i.Location() != self.Location()) || (d.isSection == true && self.Parent() != i.Parent())) {
self.Parent().Required('O');
self.Parent().RequiredActive(false);
return;
}
else {
self.Parent().Required('E');
self.Parent().RequiredActive(true);
}
});
}
},
error: HandleLocationChangeError
});
} .bind(this));
this.Role = ko.observable(data.Role);
this.TimeRangeTotal = ko.dependentObservable(function () {
var timeRangetotal = 'T: 0';
var startTime = parseInt(ko.toJS(this.ShiftStart));
var endTime = parseInt(ko.toJS(this.ShiftEnd));
if (!isNaN(startTime)) {
var duration = endTime - startTime;
var hrs = parseInt(duration / 100);
var mins = duration % 100;
if (mins == 55 || mins == 70 || mins == 85)
mins = mins - 40; //addresses deducting from a total hour (e.g. 1400 - 845)
timeRangetotal = "T: " + hrs + ":" + mins;
}
return timeRangetotal;
}, this);
}
注意 Child 对象作为一个 Dependent observable 和一个 subscribe 方法。我想在运行时添加一个子对象,因此在 DailyItems 模型中添加了 Add() 函数。我的问题是如何在 Add() 方法中满足 subscribe 方法和可靠的 observable ?
感谢有人可以提供帮助。
问候, 查图