我有一个包含不同淘汰生成内容的页面。我有一个包含数据的右侧菜单,该菜单应在页面中间加载新数据,具体取决于右侧菜单中选择的行。
所以,我可以在页面中间获取内容。它从后端加载当前内容并显示它。但是,当我在右侧面板中选择一个项目时......我想重新获取页面中间的数据并更新页面。然而,这对我来说是一个停止点。由于内容的获取是在命名空间中完成的,并且不允许使用全局变量,因此我无法重用当前的 viewModel(或者,至少我不知道如何)。因此,我正在创建一个新的 viewModel,我无法对其应用绑定,因为在加载页面时它们已经应用于相同类型的 viewModel。
这是我的 JS 代码:
Function.registerNamespace('Domain.Discussion');
$(document).ready(function () {
var repliesViewModel = Domain.Discussion.RepliesView.ReplyPage;
ko.applyBindings(repliesViewModel, document.getElementById('discussion'));
});
Domain.Discussion.RepliesView = Domain.Discussion.RepliesView || {
ReplyPage: (function () {
function Reply(data) {
var self = this;
self.ParentItemId = data.ParentItemID;
self.Body = data.Body;
self.Created = data.Created;
}
var onGetRepliesSuccess = function (replies) {
var buffer;
buffer = $.map(replies.RepliesResult, function (item) { return new Reply(item); });
viewModelReplies.replies(buffer);
};
var onGetRepliesError = function (sender, args) {
alert(args.message);
};
var viewModelReplies = {
replies: ko.observableArray([]),
loadReplies: function (parentItemId, listId) {
if (parentItemId !== null && listId !== null) {
console.log("ParentItemId: " + parentItemId + " -- ListId: " + listId);
alert("Domain.Discussion.RepliesView");
$.ajax({
url: absolutePath + "api/Replies/" + listId + "/" + parentItemId,
headers: { "Accept": "application/json; odata=verbose" },
success: onGetRepliesSuccess,
error: onGetRepliesError
})
}
},
};
viewModelReplies.loadReplies(1, '6a5ed4ad-5a7c-428f-947e-b8aa2de54489');
return viewModelReplies;
}())
};
// The method loaded by the row-click in the right panel.
Domain.Discussion.LoadDiscussionReplies2 = function (discussionListId, currentItem) {
try {
if (currentItem.ListId !== "" && currentItem.Id !== "") {
// Call the current viewModel and tell it to loadReplies
// This doesn't work since global variables are, of course, not allowed
repliesViewModel.loadReplies(currentItem.Id, currentItem.ListId);
}
}
catch (e) {
alert(e);
}
}
这是我的 HTML 代码:
<!-- page discussion -->
<div data-role="page" id="discussion">
<!-- Content -->
<div data-role="content">
<!-- ladda replies -->
<div id="replies">
<!-- ko foreach: replies -->
<div class="message message-first">
<div class="message-header">
<div class="message-header-user">
<h1>Persons Name</h1>
<h2>Subtitle</h2>
</div>
<div class="message-header-date" data-bind="text: Created"></div>
</div>
<div class="message-content">
<span data-bind="text: Body"></span>
<hr />
</div>
</div>
<!-- /ko -->
</div>
</div>
<!-- panel -->
<!-- Grupper -->
<div id="nav-panel-discussion-groups" data-role="panel" data-theme="a" data-position="right" data-display="overlay">
<!-- ko foreach: discussions -->
<ul class="nav-search" data-role="listview" data-theme="a" data-iconpos="left" data-inset="true">
<li data-role="list-divider" data-swatch="a" data-theme="a" data-form="ui-bar-a">
<div data-type='horizontal'>
<label data-bind="text: Title"></label>
<a data-bind="attr: { eventId: Id }" eventId="" name='newThread' data-role="button" data-icon='plus' data-inline="true" data-mini="true" data-theme="a" data-iconpos='notext' id="A4">New</a>
</div>
</li>
<!-- ko foreach: Threads -->
<li data-mini="true" data-icon="false" data-bind="">
<a href="#discussion" data-bind="text: Title, click: Domain.Discussion.LoadDiscussionReplies2.bind($root.Id, Id)" data-transition="slide"></a>
</li>
<!-- /ko -->
</ul>
<!-- /ko -->
</div>
<!-- /panel -->
</div>
我的问题是:如何在页面加载时重新获取数据并重新使用 viewModel。我现在完全被困住了。