【发布时间】:2015-04-26 04:23:44
【问题描述】:
我正在使用 Knockoutjs、requirejs 和 mvc4 来构建 Web 应用程序。我有一个 公司列表要显示在数据表中 我的 Common.js 如下:
require.config({
baseUrl: "/Scripts/",
paths: {
"jquery": "jquery-1.8.2",
"jqueryui": "jquery-ui-1.8.24",
"jqdatatable": "jquery.dataTables",
"bootstrapdatatable": "dataTables.bootstrap",
"KO": "knockout-2.2.0"
},
shim: {
"jqdatatable": "jquery.dataTables",
"bootstrapdatatable": "dataTables.bootstrap"
}
});
我的 CompanyGrid.js 如下所示:
define(["jquery", "jqdatatable", "bootstrapdatatable","KO"], function ($,ko) {
$(document).ready(function () {
var urlPath = window.location.pathname;
ko.applyBindings(CompanyListVM);
CompanyListVM.getComapanies();
var CompanyListVM = {
Company: ko.observableArray([]),
getComapanies: function () {
$.ajax({
type: "GET",
url: '/Company/FetchCompanies',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
self.Company(data);
ShowGrid();
},
error: function (err) {
alert(err.status + " : " + err.statusText);
}
});
},
};
}
);
}
);
function ShowGrid() {
(function ($) {
$("#liCompanyList").addClass("active");
$("#liHome").removeClass("active");
$("#liUserManagement").removeClass("active");
$("#liReport").removeClass("active");
$('#example').dataTable();
}(jQuery));
}
//Model
function Company(data) {
this.CompName = ko.observable(data.CompName);
this.CompLanguage = ko.observable(data.CompLanguage);
this.CompEmail1 = ko.observable(data.CompEmail1);
this.CreatedBy = ko.observable(dat.CreatedBy);
}
这是显示公司列表。但我想显示另一个显示用户列表的网格。我已经使用必要的更改编写了相同的函数。代码是为 Usergrid.js 提供的。
define(["jquery", "jqdatatable", "bootstrapdatatable", "KO"], function ($, ko) {
$(document).ready(function () {
ko.applyBindings(person);
ko.applyBindings(UserListVM);
UserListVM.getUsers();
var urlPath = window.location.pathname;
var UserListVM = {
User: ko.observableArray([]),
getUsers: function () {
var self = this;
$.ajax({
type: "GET",
url: '/UserManagement/FetchUsers',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
self.User(data);
ShowGrid();
},
error: function (err) {
alert(err.status + " : " + err.statusText);
}
});
},
};
}
);
}
);
function ShowUserGrid() {
(function ($) {
$("#liCompanyList").addClass("active");
$("#liHome").removeClass("active");
$("#liUserManagement").removeClass("active");
$("#liReport").removeClass("active");
$('#tblUserList').dataTable();
}(jQuery));
}
//Model
function User(data) {
this.Usr_Id = ko.observable(data.Usr_Id);
this.Usr_Name = ko.observable(data.Usr_Name);
this.Usr_Email = ko.observable(data.Usr_Email);
}
但是这次用户网格没有来。webconsole显示以下错误“TypeError: ko is undefined”
请帮助我,我对 knockout.js 很陌生。
【问题讨论】:
标签: asp.net-mvc-4 knockout.js requirejs