지옥 전체,녹아웃 구독 문제
나는 하위 모델이있는 복잡한 녹아웃 뷰 모델을 가지고 있습니다. 여기에 일어나는 DailyItems (의 관측 위치는()) 메인 뷰 모델을 통해 채워지는 드롭 다운리스트에서 값입니다 무엇
var DailyItems = function (data) {
var p = this;
this.Date = ko.observable(data.Date);
this.Required = ko.observable(data.Required);
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.WorkSegments = ko.observableArray([]);
var records = $.map(data.WorkSegments, function (x) { return new WorkShift(p, x) });
this.WorkSegments(records);
this.EnableAdd = ko.computed(function() {
return this.WorkSegments().length < 8;
}, this);
this.Add = function() {
var data = {
Parent: p,
ID: "",
Date: this.Date,
Location: UNIT_ID,
Role: "",
EmployeeRoles: this.WorkSegments()[0].EmployeeRoles(),//add the roles of the first work segment
ShiftStart: "",
ShiftEnd: ""
};
var child = new WorkShift(p, data);
this.WorkSegments.push(child);
}
this.Delete = function (item) {
this.WorkSegments.remove(item);
}
};
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.ShiftStart = ko.observable(data.ShiftStart);
this.ShiftEnd = ko.observable(data.ShiftEnd);
this.EmployeeRoles = ko.observableArray(data.EmployeeRoles);
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.EmployeeRoles(d.Roles);
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) {
for (var i = 0; i < tasks.length; i++) {
if ((d.IsSection == false && tasks[i].Location() != self.Location()) || (d.IsSection == true && self.ParentBranch() != tasks[i].ParentBranch())) {
self.Parent().Required('O');
}
else {
self.Parent().Required('E');
}
}
}
},
error: HandleLocationChangeError
});
}.bind(this));
this.Role = ko.observable(data.Role);
}
: 다음은 뷰 모델 정의입니다. 위치가 변경되면 선택한 위치, 즉 Location.Subscribe 메소드에 따라 EmployeeRoles() observablearray가 변경되어야합니다.
제 문제는이 구독 메서드는 초기 데이터로드 중에도 호출된다는 것입니다. 그래서 처음에는 불필요한 서버 호출이 있습니다. 사용자가 실제로 드롭 다운 선택을 변경할 때만 호출되기를 원합니다.
이 옵션을 얻으려면 어떤 옵션이 필요합니까?
감사합니다, Chathu 내 UI가 결합되어 다음
초기로드에서 호출되는 것을 보지 못했습니다. 코드가 누락 되었습니까? 여기 당신이 제공 한 것으로부터 창조 한 바이올린이 있습니다. http://jsfiddle.net/sujesharukil/ywEP2/ –