Knockout.js 淘汰视图模型中的脏标志不起作用
我在向viewmodel添加脏标志时遇到问题。 此Viewmodel用于填充网格。我在MVC项目中使用它来填充网格。 ViewModel的格式如下:Knockout.js 淘汰视图模型中的脏标志不起作用,knockout.js,knockout-validation,Knockout.js,Knockout Validation,我在向viewmodel添加脏标志时遇到问题。 此Viewmodel用于填充网格。我在MVC项目中使用它来填充网格。 ViewModel的格式如下: function obejctDataToSchema(object) { return { "identifiers": { "id": object.userId.toString(), "name": object.userName },
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();
}
}
}
});
}
函数obejctDataToSchema(对象){
返回{
“标识符”:{
“id”:object.userId.toString(),
“名称”:object.userName
},
“扩展”:{
“CredentialHolderAssignedCredential”:[
{
“授权因素”:{
“pin”:object.userCode
},
“扩展”:{
“CredentialsSignedPermission”:[
{
“标识符”:{
“id”:object.authorityLevel
}
}
]
}
}
],
“凭证持有人签名分录”:[
{
“标识符”:{
“id”:1
}
}
]
}
}
};
功能用户(数据){
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);
此.zwaveDoorLock=0;
归还这个;
}
函数userDefaults(){
返回新用户({userId:0,userName:'',分区:true,userCode:'',authorityLevel:0,zwaveDoorLock:0});
}
函数schemaDataObject(数据){
返回新用户({
userId:parseInt(data.identifiers.id),
用户名:data.identifiers.name,
分区:data.expand.CredentialHolderAsignedToPartition[0].identifiers.id,
用户代码:data.expand.CredentialHolderAsignedCredential[0]。authFactor.pin,
authorityLevel:data.expand.CredentialHolderAsignedCredential[0]。expand.CredentialAsignedPermission[0]。identifiers.id
});
}
var usersModel=函数(intData){
var self=这个;
self.authorityLevel=authorityLevel;
self.zwaveDoorLock=zwaveDoorLock;
self.selectedItem=ko.observable(userDefaults());
self.operation=ko.observable();
自我计数=可观察到的ko(0);
self.list=ko.observearray(ko.utils.arrayMap(intData,function,item){
返回SchemaDataObject(项目);
}));
self.update=函数(){
//检查是否有效
if(self.errors().length>0){
self.errors.showAllMessages();
logger.logError(“保存前更正所有错误”、“”、“”、“”和“错误”);
返回;
}
否则{
调试器;
var item=self.selectedItem();
var jsItem=ko.toJS(项目);
var match=ko.utils.arrayFirst(self.list(),函数(项){
返回item.userName()==jsItem.userName;
});
如果(匹配){
logger.logError(“用户”+“”+jsItem.userName++“已退出”、、、、“错误”);
返回;
}
保存数据(项目)
$(“#编辑模板”).css(“显示”、“无”);
$(“#displayAdd”).css(“display”、“none”);
}
};
函数saveData(项){
var jsItem=ko.toJS(项目);
jsItem.partition=+(jsItem.partition);
$.each($(“#用户网格”).data(“kendoGrid”).dataSource.\u数据,函数(索引,用户数据){
if(u.find(self.authorityLevel,函数(e){return e.name==userData.authorityLevel;})!=null)
userData.authorityLevel=uzy.find(self.authorityLevels,函数(e){returne e.name==userData.authorityLevel;}).id;
});
var objectData={ObjectTypeId:ObjectTypeId,EntityIdentifier:jsItem.userId,EntityValue:JSON.stringify(新的obejctDataToSchema(jsItem)),ObjectTypeName:“User”};
doAjax.request({
contentType:'应用程序/json',
数据类型:“json”,
键入:“POST”,url:“SystemConfig/SaveEntityData”,数据:JSON.stringify({accountNumber:accountNumber,objectData:objectData}),callbackSuccess:function(returnData){
如果(返回数据){
if(self.operation=='edit'){
$(“#编辑模板”).css(“显示”、“无”);
var htmlString=$(“#编辑模板”)[0];
$.each(self.list(),函数(idx,elem){
if(elem.userId()==jsItem.userId){
自.列表.删除(elem);
返回false;
}
});
$(“#setingsheader”).append(htmlString);
}
self.list.push(新用户(jsItem));
if(self.list().length
请帮助我,并建议我如何添加一个脏标志到这个
谢谢你你想要什么样的脏旗来跟踪模型上的更改?如果是这样,请检查我在这里做了什么:你的viewmodel非常简单。我的viewmodel完全不同。你能用我的viewmodel更新小提琴吗?