2015-02-24 54 views
0

我使用Knockoutjs,requirejs和mvc4用于构建Web application.I有 公司列表jQuery的数据表中要显示其数据见下表 我Common.js给出:与knockoutjs,requirejs和mvc4

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); 
} 

这显示list.But我想告诉另一电网这表明用户已经list.I使用必要changes.The编写的代码相同功能的公司:

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下面给出给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); 

} 

但此时用户网格是不显示以下错误coming.The Web控制台“类型错误:KO是未定义”

请帮助我,我很新的knockout.js。

回答

0

您正在使用require.js且参数不匹配。

define(["jquery", "jqdatatable", "bootstrapdatatable", "KO"], function ($, ko) { 

jquery被加载到$

jqdatatable被加载到ko

+0

感谢。但我的疑问是,它在companygrid中完美运行。 js.it仅在usergrid中显示错误。请帮助我并清除我的疑问 – amitabha 2015-02-24 14:43:53