2015-02-24 101 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