我的目标是将用户选择保存在 KendoUIWeb-Multiselect 的多选列表中。我觉得跟踪列表更改的最简单方法就是删除多对多关系中的所有内容并添加新选择的项目。 当我迭代多对多关系并将每个实体的方面设置为“setDeleted”时,迭代开始通过循环传递“未定义”对象。从表面上看,设置要删除的联合对象似乎会影响 Breeze 中列表中的其他对象。还有另一种方法可以迭代删除所有连接对象吗?
注意: 如果我不调用此方法,我可以毫无问题地迭代连接模型。
public class Course
{
public int Id { get;set; }
public string Name { get;set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class StudentCourse
{
public int StudentId { get;set; }
public virtual Student Student { get;set; }
public int CourseId { get;set; }
public virtual Course Course { get;set; }
}
以下代码完美运行(为 KendoUI 多选设置多选下拉列表):
var getSelectedCourses = function () {
var selected = [];
ko.utils.arrayForEach(student().studentCourses(), function(course) {
var courseId = parseInt(course.courseId());
selected.push(courseId);
});
return selected;
};
以下代码体现了该问题。前几次迭代似乎进行得很好,但之后“未定义”作为“类(class)”参数传递
var removeListItems = function () {
if (courseHasChanges()) {
//Remove all list items
ko.utils.arrayForEach(student().studentCourses(), function(studentCourse) {
if (studentCourse) { //passing undefined after 2nd or 3rd iteration
studentCourse.entityAspect.setDeleted();
}
});
}
};
最佳答案
我相信当您调用 setDeleted()
迭代它时,Breeze 将从 studentCourses()
数组中删除该类(class)。
也许使用 slice(0)
克隆原始 studentCourses()
数组可能会起作用:(未经测试)
var removeListItems = function () {
if (courseHasChanges()) {
//Remove all list items
ko.utils.arrayForEach(student().studentCourses().slice(0), function(course) {
if (course) { //passing undefined after 2nd or 3rd iteration
course.entityAspect.setDeleted();
}
});
}
};
关于javascript - Breeze 多对多关系以及在迭代问题上将 Aspect 设置为 'setDeleted',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16091761/