Jquery 在knockout.js中为ViewModel创建多个实例
我正在使用knockout创建一个寻呼机控件。我有一个viewModel(PaginatorViewModel),它具有“currentPageIndex”、“itemsPerPage”、“totalRecords”等属性。 在每个页面中,我有两个分页控件,一个在页面顶部,另一个在页面底部 在某些情况下,我有选项卡,每个选项卡中有两个分页控件(顶部和底部) 当我在Tab1中并移动到第2页(currentPageIndex=2)时,Tab2中的分页控件也将currentPageIndex显示为2 我想在所有选项卡中使用PaginatorViewModel,但希望维护多个实例,即每个选项卡一个实例 我怎样才能做到这一点 这是我的ViewModelJquery 在knockout.js中为ViewModel创建多个实例,jquery,knockout.js,Jquery,Knockout.js,我正在使用knockout创建一个寻呼机控件。我有一个viewModel(PaginatorViewModel),它具有“currentPageIndex”、“itemsPerPage”、“totalRecords”等属性。 在每个页面中,我有两个分页控件,一个在页面顶部,另一个在页面底部 在某些情况下,我有选项卡,每个选项卡中有两个分页控件(顶部和底部) 当我在Tab1中并移动到第2页(currentPageIndex=2)时,Tab2中的分页控件也将currentPageIndex显示为2
var CustomPaginator = {
//The current page index of the post being displayed.
currentPageIndex: ko.observable(1),
//specifies the page options
pageOptions : ko.observableArray([25, 50, 100, 200]),
//specifies the PageOptions will be shown or not.
showOptions : ko.observable(true),
//The maximum number of topics displayed per page.
itemsPerPage : ko.observable(25),
//Specifies the total no of records
totalRecords : ko.observable(1),
isVisible : ko.observable(false),
//pageIndex is bounded to the text box, and the CurrentPageIndex contains the actual value.
// this is to avoid the notifying the subscribers when the user enters the out of range values
// like 0,and N , where N > no of pages
pageIndex : ko.observable(1)
};
我怎样才能为它创建一个实例
谢谢,
Ramesh为每个分页器创建一个viewmodel,然后将每个分页器绑定到要在其中使用的页面元素:
var page1 = new CustomPaginator();
ko.applyBindings(page1, $('#page1')[0]);
每个人都这样做。如果需要,还可以将同一viewmodel绑定到页面的不同部分
您需要更改定义视图模型的方式,以便可以创建视图模型的新实例:
var CustomPaginator = function()
{
//your view model properies here
}
为每个分页器创建viewmodel,然后将每个分页器绑定到要在其中使用的页面元素:
var page1 = new CustomPaginator();
ko.applyBindings(page1, $('#page1')[0]);
每个人都这样做。如果需要,还可以将同一viewmodel绑定到页面的不同部分
您需要更改定义视图模型的方式,以便可以创建视图模型的新实例:
var CustomPaginator = function()
{
//your view model properies here
}
无法创建类似以下var page1=new CustomPaginator()的实例;然后需要将视图模型定义为函数。我已经更新了我的答案。@user2287645您的示例具有custompaginator js对象文字。您需要将其定义为一个函数。如果您来自c#或java等强类型语言背景,则在js中创建类与定义函数然后创建该函数的实例相同,您将拥有一个对象。无法创建下面的var page1=new CustomPaginator()这样的实例;然后需要将视图模型定义为函数。我已经更新了我的答案。@user2287645您的示例具有custompaginator js对象文字。您需要将其定义为一个函数。如果您来自c#或java等强类型语言背景,那么在js中创建类与定义函数然后创建该函数的实例相同,您将拥有一个对象。