无法处理绑定 - 未定义

时间:2016-02-11 17:07:57

标签: javascript knockout.js

我正在努力扩展我对KnockoutJS的了解(我对此有很高的了解)。我正在建立一个前端,通过Web API与我的业务逻辑/数据访问进行对话。我能够从API中读取,但我正在尝试构建插入表单,并收到以下错误:

未捕获的ReferenceError:无法处理绑定“value:function(){return startTime}”
消息:未定义startTime

这是LessonViewModel.js

var lessonRegisterViewModel;

function Lesson(id, name, teacher, room, subject, startTime, endTime) {
    var self = this;
    self.Id = ko.observable(id);
    self.Name = ko.observable(name);
    self.Teacher = ko.observable(teacher);
    self.Room = ko.observable(room);
    self.Subject = ko.observable(subject);
    self.StartTime = ko.observable(startTime);
    self.EndTime = ko.observable(endTime);
    self.addLesson = function() {
        var dataObject = ko.toJSON(this);
        $.ajax({
            url: '/api/Lessons',
            type: 'post',
            data: dataObject,
            contentType: 'application/json',
            success: function(data) {
                lessonRegisterViewModel.lessonListViewModel.lessons.push(new Lesson(data.Id, data.Name, data.Teacher, data.Room, data.Subject, data.StartTime, data.EndTime));
                self.Id(null);
                self.Name('');
                self.Teacher('');
                self.Room('');
                self.Subject('');
                self.StartTime('');
                self.EndTime('');
            }
        });
    }
}

function LessonList() {
    var self = this;
    self.lessons = ko.observableArray([]);
    self.getLessons = function() {
        self.lessons.removeAll();
        $.getJSON('/api/Lessons', function(data) {
            $.each(data, function(key, value) {
                self.lessons.push(new Lesson(value.id, value.name, value.teacher, value.room, value.subject, value.startTime, value.endTime));
                console.log(self);
            });
        });
    };
    self.removeLesson = function(lesson) {
        $.ajax({
            url: '/api/Lessons/' + lesson.Id(),
            type: 'delete',
            contentType: 'application/json',
            success: function() {
                self.lessons.remove(lesson);
            }
        });
    }
}
lessonRegisterViewModel = {
    addLessonViewModel: new Lesson(),
    lessonListViewModel: new LessonList()
};
$(document).ready(function() {
    // bind view model to referring view
    ko.applyBindings(lessonRegisterViewModel);
    // load lesson data
    lessonRegisterViewModel.lessonListViewModel.getLessons();
});

以下是我的观点:

<form role="form">
    <div class="form-group">
        <label for="inpLessonName">Lesson Name</label>
        <input id="inpLessonName" type="text" class="form-control" data-bind="value: name" />
    </div>

    <div class="form-group">
        <label for="inpStartTime">Start Time</label>
        <input id="inpStartTime" type="datetime-local" class="form-control" data-bind="value: StartTime" />
    </div>
</form>

<input type="button" id="btnAddLesson" class="btn btn-info" value="Add Lessons" data-bind="click: " /> 

如果你能指出我的错误,以及你预见到的任何其他问题,我真的很感激!

0 个答案:

没有答案