2012-04-06 19 views
0

我遵循MVC4 SPA演練的例子,並創建了一個簡單的應用程序,其中我拉下10個主題的列表,讓用戶標記爲安全或不良,然後保存更改。現在,當用戶保存更改時,我想重新加載列表,其中包含接下來要處理的10個主題。我將如何做到這一點?提交更新結果數據源後刷新淘汰賽模型

這是我的觀點的數據模型:

function TopicsViewModel() { 
    // Data 
    var self = this; 
    self.dataSource = upshot.dataSources.UnsafeTopics.refresh(); 
    self.topics = self.dataSource.getEntities(); 

    // Operations 
    self.saveAll = function() { 
     self.dataSource.commitChanges(); 
    } 
    self.revertAll = function() { 
     self.dataSource.revertChanges(); 
    } 
} 

查看:

@(Html.UpshotContext(bufferChanges: true).DataSource<TopicDataController>(x => x.GetUnsafeTopics())) 

<button id="saveAll" data-bind="click: saveAll">Commit changes</button> 


<ul data-bind="foreach: topics"> 
    <li data-bind="css: { updated: IsUpdated }"> 
     <strong class="topicItem" data-bind="text: TopicName"></strong> 
     <label><input class="bad" type="checkbox" data-bind="checked: IsBad" /> is bad</label> 
     <label><input class="safe" type="checkbox" data-bind="checked: IsSafe"/> is safe</label> 
    </li> 
</ul> 

<script src="~/Scripts/App/TopicsViewModel.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(function() { 
     ko.applyBindings(new TopicsViewModel()); 
    }); 

</script> 

控制器:

public class TopicDataController : DbDataController<SocialViewEntities> 
    { 
     public IQueryable<Topic> GetUnsafeTopics() 
     { 
      return DbContext.Topics.Where<Topic>(t => t.IsBad == false).Where<Topic>(t => t.IsSafe == false).Take<Topic>(10); 
     } 

     public void UpdateTopic(Topic topic) { UpdateEntity(topic); } 

    } 

回答

3

基本上你正在做什麼是分頁。壞消息是顯然最新版本的Upshot(1.0.0.2)沒有一個名爲PagingModel的對象,這是BigShelf示例用於分頁的對象。

好消息是,你可以嘗試下載該例子並提取地處upshot.knockout.extensions.js文件(包括在下載上面指出)的PagingModel代碼,看看它是否有最新版本的作品結果(1.0.0.2)。我會親自嘗試,並用任何結果更新你。


更新: 我已經挖得更深一些,發現該PagingModel對象確實與1.0.0.2工作,它可能是一個好主意,因爲它簡化了一切,用它在你的情況(爲您提供可以綁定到前進到下一個頁面的功能,轉到最後一個等等)

但是PagingModel並不是真正必需的,因爲您需要執行分頁(跳過和取出功能)的所有內容都已存在dataSource對象。下面是如何在沒有PagingModel的情況下做到這一點的例子。

首先,添加而觀察到的當前頁:當初始化

self.currentPage = ko.observable(); 

其次,不要刷新您的數據源,而不是設置頁面參數,以便在你的數據庫每個主題都牽強,和然後刷新數據源。這樣做是每當當前頁屬性更改:

self.currentPage.subscribe(function() { 
    this.dataSource.setPaging({ 
    skip: (this.currentPage() - 1) * 10, 
    take: 10, includeTotalCount: true }); 
    this.dataSource.refresh(); 
}).bind(self); 

// Trigger the refresh 
self.currentPage(1); 

然後,你的視圖模型的白水功能更改爲以下觸發刷新到下一個頁面。

// Operations 
self.saveAll = function() { 
    self.dataSource.commitChanges(); 
    self.currentPage(self.currentPage() + 1); 
} 

記住:從數據源起始線刪除刷新()!

希望它有幫助!