我在向viewmodel添加脏标志时遇到问题。 此Viewmodel用于填充网格。我在MVC项目中使用它来填充网格。 ViewModel采用以下格式:
function obejctDataToSchema(object) {
return {
"identifiers": {
"id": object.userId.toString(),
"name": object.userName
},
"expand": {
"CredentialHolderAssignedCredential": [
{
"authFactor": {
"pin": object.userCode
},
"expand": {
"CredentialAssignedPermission": [
{
"identifiers": {
"id": object.authorityLevel
}
}
]
}
}
],
"CredentialHolderAssignedToPartition": [
{
"identifiers": {
"id": 1
}
}
]
}
}
};
function User(data) {
this.userId = ko.observable(data.userId);
this.userName = ko.observable(data.userName).extend({ required: true });
this.partition = ko.observable(data.partition);
this.userCode = ko.observable(data.userCode).extend({ min: 1000, max: 9999 }, { required: true });
this.authorityLevel = ko.observable(data.authorityLevel);
this.zwaveDoorLock = 0;
return this;
}
function userDefaults() {
return new User({ userId: 0, userName: '', partition: true, userCode: '', authorityLevel: 0, zwaveDoorLock: 0 });
}
function schemaDatatoObject(data) {
return new User({
userId: parseInt(data.identifiers.id),
userName: data.identifiers.name,
partition: data.expand.CredentialHolderAssignedToPartition[0].identifiers.id,
userCode: data.expand.CredentialHolderAssignedCredential[0].authFactor.pin,
authorityLevel: data.expand.CredentialHolderAssignedCredential[0].expand.CredentialAssignedPermission[0].identifiers.id
});
}
var usersModel = function (intData) {
var self = this;
self.authorityLevels = authorityLevel;
self.zwaveDoorLock = zwaveDoorLock;
self.selectedItem = ko.observable(userDefaults());
self.operation = ko.observable();
self.count = ko.observable(0);
self.list = ko.observableArray(ko.utils.arrayMap(intData, function (item) {
return schemaDatatoObject(item);
}));
self.update = function () {
// check if valid
if (self.errors().length > 0) {
self.errors.showAllMessages();
logger.logError("Correct All Errors Before Saving", "", "", "error");
return;
}
else {
debugger;
var item = self.selectedItem();
var jsItem = ko.toJS(item);
var match = ko.utils.arrayFirst(self.list(), function (item) {
return item.userName() === jsItem.userName;
});
if (match) {
logger.logError("User" + " " + jsItem.userName + " " + "already exits", "", "", "error");
return;
}
saveData(item)
$("#editTemplate").css("display", "none");
$("#displayAdd").css("display", "none");
}
};
function saveData(item) {
var jsItem = ko.toJS(item);
jsItem.partition = +(jsItem.partition);
$.each($("#userGrid").data("kendoGrid").dataSource._data, function (index, userData) {
if (_.find(self.authorityLevels, function (e) { return e.name == userData.authorityLevel; }) != null)
userData.authorityLevel = _.find(self.authorityLevels, function (e) { return e.name == userData.authorityLevel; }).id;
});
var objectData = { ObjectTypeId: objectTypeId, EntityIdentifier: jsItem.userId, EntityValue: JSON.stringify(new obejctDataToSchema(jsItem)), ObjectTypeName: "User" };
doAjax.request({
contentType: 'application/json',
dataType: 'json',
type: "POST", url: "SystemConfig/SaveEntityData", data: JSON.stringify({ accountNumber: accountNumber, objectData: objectData }), callbackSuccess: function (returnData) {
if (returnData) {
if (self.operation == 'edit') {
$("#editTemplate").css("display", "none");
var htmlString = $("#editTemplate")[0];
$.each(self.list(), function (idx, elem) {
if (elem.userId() === jsItem.userId) {
self.list.remove(elem);
return false;
}
});
$("#settingsHeader").append(htmlString);
}
self.list.push(new User(jsItem));
if (self.list().length < maxUsers) {
createNew();
}
}
}
});
}
请帮帮我,建议如何为此添加脏标志。
谢谢