2012-04-06 38 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); 
} 

记住:从数据源起始线删除刷新()!

希望它有帮助!