2013-04-08 27 views
1

我有我在視圖中顯示的聯繫人組列表。 當我點擊一個聯繫人組時,我想更改當前哈希並加載關聯的聯繫人。DurandalJS - 無視圖模式刷新導航

說,我3個觸頭組

所有(無ID)

好友(ID:= 1)

家庭(ID:= 2)

其他( Id:= 3)

如果我點擊好友,網址需要更改爲#/ people/1並且需要使用參數(Id = 1)調用回調。如果點擊全部,則不應傳遞Id(#/ people)加載所有聯繫人。

喜歡的東西:

router.when("#/people/:id", function (routeData) { 
    console.log(routeData.Id); //Output: 1 

    // Load contacts by id 
}); 

router.when("#/people", function() { 
    // Load all contacts 
}); 

function onContactGroupClick(contactGroup) { 
    router.navigateToWithoutReset("#/people/" + contactGroup.Id); 
} 

的原因是,我想有一箇中央位置,如果呼叫是通過代碼或手工製作也沒關係(變更網址,或點擊超鏈接)。

一個很好的例子是的Gmail /觸點

如果我使用router.navigateTo從DurandalJS,整個視圖模型將被重新加載。有沒有辦法與DurandalJS做到這一點,但不刷新視圖模型?

回答

1

你是什麼意思由中央位置?

從我明白這是你在找什麼:

define(["services/dataservice"], function (dataservice) { 
     var allContacts = ko.observableArray(); 
     var friendsContacts = ko.observableArray(); 
     var contacts = ko.observableArray(); 
     var loadedAllData= false; 
     var loadedFriends = false; 
     var vm = { 
      activate: activate, 
      contacts: contacts, 
     }; 
     return vm; 

     function activate(routeData) { 
      var id = parseInt(routeData.id); 
      if(id === 1) 
      { 
       if(loadedFriends) 
       { 
        //push friends observable into contacts 
       } 
       else 
       { 
        //load friends from server and set loadedFriends to true. 
       } 
      } 
      else 
      { 
       if(loadedAll) 
       { 
        //push all contacts this into the contacts array 
       } 
       else 
       { 
        //load from the webserver, and also filter friends and put them into friends observable. Here set both loadedAll and loadedFriends to true. 
       } 
      } 
     } 
    } 
); 

所以,如果你與聯繫人結合你的觀點,然後當你第一次來到這個畫面尋找從URL或從朋友一個超鏈接,它應該只是加載來自Web服務器的朋友數據,在隨後的回覆到相同的屏幕上的朋友數據應該是有它不會一次又一次加載。 Durandal將使用相同的虛擬機,並且不會重置標誌和可觀察對象(除非您手動進入並點擊瀏覽器刷新按鈕)。