これが私のシナリオです。
ページの翻訳を処理するための別の KnockoutJS バインディング プロバイダーを作成したいと考えています。このプロバイダーは、ページの読み込み時にdata-alias
存在するか、ページの読み込み後に作成された (おそらくノックアウトによってレンダリングされたサブテンプレートで作成された) 要素の属性を読み取り、翻訳を取得します。エイリアスに適用し、要素のテキストとして適用します。ko.applyBindings(model, element);
ページのdata-bind
属性を処理するために個別に呼び出すことができるようにしたいのですがko.applyBindings
、属性に翻訳を適用できるように別の呼び出しを行いたいと考えていdata-alias
ます。両方をサポートし、独立して動作させる方法を知っている人はいますか?
プロセスの進め方の例を次に示します。
HTML マークアップ/テンプレート:
<fieldset>
<legend data-alias="SomeTitle"></legend>
<div class="row-container">
<div class="control-group">
<label class="control-label" data-alias="LabelAlias"></label>
<div class="controls">
<input type="text" data-bind="value: SomeObservable" />
</div>
</div>
<div class="control-group">
<label class="control-label" data-alias="AnotherLabelAlias"></label>
<div class="controls">
<input type="text" data-bind="value: AnotherObservable" />
</div>
</div>
</div>
</fieldset>
ページの読み込み時に、data-alias のバインディング プロバイダーを使用してバインディングを適用するための呼び出しがあります。
<script type="text/javascript">
$(function() {
// Lets assume translations is a dictionary of alias to translation
// that is loaded with the page synchronously
ko.applyBindings(translations);
});
</script>
また、ページの読み込み時に、ページに必要なデータを取得するためのサービス呼び出しがあり、取得時にデータにバインディングが適用されます。
<script type="text/javascript">
$(function() {
$.ajax({...}) // Omitted for brevity
.done(function(data) {
var viewModel = new my.namespace.SomeViewModel(data);
ko.applyBindings(viewModel);
});
});
</script>
意図した効果:
- ページが読み込まれます。
- 翻訳が適用されます。
- データを取得するためにサービス コールが開始されます。
- サービスコールが返ります。
- データはビュー モデルに変換され、ページにバインドされます (翻訳は保持されます)。