我遵循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); }
}