我在 Knockout 视图模型中有一个数组,如下所示:
this.Activities = ko.observableArray([
{ "date": "28/11/2012 00:00:00",
"activities": [
{ "company": "BOW",
"description": "Backup Checks",
"length": "60"
},
{ "company": "AMS",
"description": "Data Request",
"length": "135"
},
]},
{ "date": "30/11/2012 00:00:00",
"activities": [
{ "company": "BOW",
"description": "Backup Checks",
"length": "60"
},
{ "company": "SLGT",
"description": "Software Development",
"length": "240"
},
{ "company": "BOW",
"description": "Data Request",
"length": "30"
},
]},
]);
我使用此代码向其中添加一个新元素:
this.Activities.push(new NewActivity(company, description, length, fullDate));
其中使用 NewActivity 功能:
function NewActivity(company, description, length, date) {
this.date = date;
this.activities = [{ "company": company, "description": description, "length": length }];
}
它工作正常。但是,它每次被释放时都会创建一个全新的对象。当代码检查已创建对象的日期时,我需要实现一个条件。如果新创建的对象具有相同的日期,则应将活动详细信息添加到该日期的活动数组中的活动数组中。
我该怎么做?
活动数组的所有数据都来自 MVC 应用程序中强类型视图中的模型:
this.Activities = ko.observableArray([
@foreach (ActivitySet aSet in Model)
{
@:{ "date": "@aSet.Date",
@:"activities": [
foreach(Activity a in aSet.Activities)
{
@:{ "company": "@a.Companies.Select(c => c.Title).Single()",
@:"description": "@a.Descriptions.Select(c => c.Title).Single()",
@:"length": "@a.LengthInMinutes"
@:},
}
@:]},
}
]);