gpt4 book ai didi

javascript - KnockoutJS - 绑定(bind)不会使用映射插件更新

转载 作者:行者123 更新时间:2023-11-30 17:59:06 25 4
gpt4 key购买 nike

我在 Knockout 中有一个 View 模型,它是使用 mapping 插件映射的。我需要这样做,因为将使用 Ajax 调用从服务器检索数据(JSON 对象)。

我的问题是,当我修改表单时,对象中的映射数据没有更新...

您还可以在以下 JSFiddle 中看到这一点:http://jsfiddle.net/etiennenoel/4EXSy/6/

这是我从服务器检索到的对象:

var dataContent = 
[
{
playerId: 2,
playerName: "allo",
evaluatedExercises:
[
{
id: 1,
evaluationExerciseId: 1,
numberOfTries: 6,
tries: [
{
id: 0,
number: 0,
result: 0
}
]
}
]
}
]

这是完整的 javascript View 模型:

function Try(id, number, result) {
var self = this;

self.id = id;
self.number = number;
self.result = result;
}

function ExerciseResult(id, evaluationExerciseId) {
var self = this;

self.id = id;
self.evaluationExerciseId = evaluationExerciseId;
self.tries = [];
}

function PlayerEvaluation(playerId, playerName) {
var self = this;

self.playerId = playerId;
self.playerName = playerName
self.evaluatedExercises = [];
}

function appViewModel() {
var self = this;

self.playersEvaluation = ko.observableArray();

self.exportToJSON = ko.computed(function() {
return ko.toJSON(self.playersEvaluation);
}, this);

}

var viewModel = new appViewModel();

var dataContent =
[
{
playerId: 2,
playerName: "allo",
evaluatedExercises:
[
{
id: 1,
evaluationExerciseId: 1,
numberOfTries: 6,
tries: [
{
id: 0,
number: 0,
result: 0
}
]
}
]
}
]

for(i = 0; i < dataContent.length; i++) {
playerEvaluation = new PlayerEvaluation(dataContent[i].playerId, dataContent[i].playerName);

evaluatedExercises = dataContent[i].evaluatedExercises;

for(j = 0; j < evaluatedExercises.length; j++) {
exerciseResult = new ExerciseResult(evaluatedExercises[j].id, evaluatedExercises[j].evaluationExerciseId);
tries = evaluatedExercises[j].tries;

for(k = 0; k < tries.length; k++) {
exerciseTry = new Try(tries[k].id, tries[k].number, tries[k].result)

exerciseResult.tries.push(exerciseTry);
}

playerEvaluation.evaluatedExercises.push(exerciseResult);
}

viewModel.playersEvaluation.push(playerEvaluation);
}

ko.mapping.fromJS(viewModel.playersEvaluation);
ko.applyBindings(viewModel)

此对象使用 KnockoutJS 映射到 html 中:

    <table class="table table-hover table-bordered">
<thead> <!-- This will be generated with Twig so it is normal that is does not correspond to the data below -->
<tr>
<th>Nom</th>
<th colspan="6" style="text-align:center">Élément #1</th>

</tr>
<tr>
<th></th>

<th class="try">
1
</th>
<th class="try">
2
</th>
<th class="try">
3
</th>
<th class="try">
4
</th>
<th class="try">
5
</th>
<th class="try">
6
</th>

</tr>
</thead>
<tbody data-bind="foreach: playersEvaluation">
<tr data-bind="">
<td data-bind="text: $data.playerName">
</td>
<!-- ko foreach: $data.evaluatedExercises -->
<!-- ko foreach: $data.tries -->
<td>
<input type="text" data-bind="value: $data.result" />
</td>
<!-- /ko -->
<!-- /ko -->
</tr>
</tbody>
</table>


<div data-bind="text: exportToJSON()">

</div>

当我修改输入时,我希望更改反射(reflect)在我的 viewModel 的 self.playersEvaluation 变量中。为此,我创建了一个计算函数,在表格下方的 div 中显示 playersEvaluation 的内容。

但是,div 没有得到更新,因此我得出的结论是映射一定不正确。

我的映射哪里出错了?

最佳答案

我对映射插件的经验并不丰富,但我相信您应该让它为您构建 View 模型结构(因为您似乎在 View 模型的任何级别都没有任何更复杂的逻辑)

http://knockoutjs.com/documentation/plugins-mapping.html

function appViewModel() {
var self = this;
self.playersEvaluation;
}

var viewModel = new appViewModel();

// this creates observables for everything in dataContent. the result itself is an observable
// which you can set in your top level view model
viewModel.playersEvaluation = ko.mapping.fromJS(dataContent)
ko.applyBindings(viewModel);

fiddle :http://jsfiddle.net/4EXSy/8/

关于javascript - KnockoutJS - 绑定(bind)不会使用映射插件更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com