Я могу быть слишком сложным, но у меня есть следующая ситуация, когда мне нужно увидеть, равен ли "ResourceScheduleType" некоторому числу. Я думаю, что комментарии в коде лучше всего иллюстрируют то, что я пытаюсь сделать. По какой-то причине моя переменная "matching" не дает мне свойство, в котором я нуждаюсь. Я бы хотел уйти от использования jQuery grep.
class MyCalendarVM {
CalendarPoints: MyCalendarPoints[];
}
class MyCalendarPoints {
ResourceScheduleType: number;
aDate: string;
}
class MyType {
MyName: string;
}
$(document).ready(() => {
$.get("/Calendar/GetMonthCalendar", null, (data: MyCalendarVM) => {
if (data.CalendarPoints.length == 0) {
(<any>$('#date')).datepicker();
} else {
$(<any>data.CalendarPoints).each(
(<any>$("#date")).datepicker({
beforeShowDay: function (date) {
var result = new Array(true, '', null);
var matching = <MyCalendarPoints[]>$.grep(data.CalendarPoints, function (event) {
return event.Date.valueOf() === date.valueOf();
}, false);
//TODO: make determination for blue or yellow dot
if (matching.length) {
var classes = "";
// if matching.ResourceScheduleTypeId == 3
// classes += "yellowDot";
// if matching.ResourceScheduleTypeId == 1 || 2 || 9
// classes += " blueDot";
result = [true, classes, null];
}
return result;
},
onSelect: function (dateText) {
var date,
selectedDate = new Date(dateText),
i = 0,
event = null;
while (i < data.CalendarPoints.length && !event) {
date = data.CalendarPoints[i].aDate;
if (selectedDate.valueOf() === date.valueOf()) {
event = data.CalendarPoints[i];
}
i++;
}
if (event) {
alert(event.Title);
}
}
})
);
}
});
});
Похоже, что matching
- это массив, поэтому вам нужно получить доступ к нему по индексу до того, как будут доступны свойства определенного объекта MyCalendarPoints
.
matching[0].ResourceScheduleTypeId // MyCalendarPoints
Не
matching.ResourceScheduleTypeId // MyCalendarPoints[]
Я почерпнул это из рассмотрения вашего актера:
<MyCalendarPoints[]>
Если добавить определения для JQuery и JQuery-UI, вы можете избавиться от всех any
-casts.
///<reference path='jquery-1.8.d.ts'/>
///<reference path='jqueryui-1.9.d.ts'/>
Вам также необходимо изменить
$(<any>data.CalendarPoints).each(
в
$(data.CalendarPoints).each(() =>
поскольку .each()
ожидает функцию.
Вы можете найти .d.ts
-files в github Бориса Янкова.