2013-04-16 86 views
0

我正在使用淘汰賽創建傳呼控制。我有一個viewModel(PaginatorViewModel),它具有屬性「currentPageIndex」,「itemsPerPage」,「totalRecords」等 在每一頁,我有兩個分頁控制一個在TOP和另一個在頁面的底部。在knockout.js中爲ViewModel創建多個實例

在某些情況下,我有選項卡,在每個選項卡中我有兩個分頁控件(TOP和Bottom)。

當我在TAB1和移動到第2頁(currentPageIndex = 2),分頁控制在TAB2也顯示currentPageIndex爲2

我想使用PaginatorViewModel在所有的突出部,但要維護多個實例,例如每個選項卡的一個實例。

我該如何做到這一點。

這裏是我的ViewModel,

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

如何爲這個創建一個實例。

感謝, 拉梅什

回答

3

每個分頁程序創建一個視圖模型,然後綁定每一個到你希望它在使用的頁面的元素:

var page1 = new CustomPaginator(); 
ko.applyBindings(page1, $('#page1')[0]); 

這樣做對每一個。您也可以將相同的viewmodel綁定到頁面的不同部分,如果這是您想要做的事情。

你需要改變你如何定義你的瀏覽模式,讓你可以創建它的新的實例:

var CustomPaginator = function() 
{ 
    //your view model properies here 
} 
+0

無法創建實例,如下面** VAR第1頁=新CustomPaginator(); * * – Ramesh

+0

然後,您需要將您的視圖模型定義爲一個函數。我已經更新了我的答案。 –

+0

@ user2287645您的示例具有custompaginator js對象字面值。你需要將它定義爲一個函數。如果你來自像c#或java這樣的強類型語言背景,在js中創建一個類,就像定義一個函數然後創建該函數的一個實例一樣,你將擁有一個對象。 –