特定の開始データのセットを持つコンポーネントがあります。
data: function (){
return {
modalBodyDisplay: 'getUserInput', // possible values: 'getUserInput', 'confirmGeocodedValue'
submitButtonText: 'Lookup', // possible values 'Lookup', 'Yes'
addressToConfirm: null,
bestViewedByTheseBounds: null,
location:{
name: null,
address: null,
position: null
}
}
これはモーダルウィンドウのデータなので、表示されたらこのデータから始めたいと思います。ユーザーがウィンドウからキャンセルした場合、すべてのデータをこれにリセットしたいと思います。
データをリセットするメソッドを作成して、すべてのデータプロパティを手動で元に戻すことができることはわかっています。
reset: function (){
this.modalBodyDisplay = 'getUserInput';
this.submitButtonText = 'Lookup';
this.addressToConfirm = null;
this.bestViewedByTheseBounds = null;
this.location = {
name: null,
address: null,
position: null
};
}
しかし、これは本当にずさんなようです。つまり、コンポーネントのデータプロパティに変更を加えた場合は、resetメソッドの構造を更新することを忘れないようにする必要があります。それは小さなモジュラーコンポーネントなので、それは絶対に恐ろしいことではありませんが、それは私の脳の最適化部分を悲鳴を上げさせます。
私がうまくいくと思った解決策は、ready
メソッドの初期データプロパティを取得し、その保存されたデータを使用してコンポーネントをリセットすることです。
data: function (){
return {
modalBodyDisplay: 'getUserInput',
submitButtonText: 'Lookup',
addressToConfirm: null,
bestViewedByTheseBounds: null,
location:{
name: null,
address: null,
position: null
},
// new property for holding the initial component configuration
initialDataConfiguration: null
}
},
ready: function (){
// grabbing this here so that we can reset the data when we close the window.
this.initialDataConfiguration = this.$data;
},
methods:{
resetWindow: function (){
// set the data for the component back to the original configuration
this.$data = this.initialDataConfiguration;
}
}
しかし、initialDataConfiguration
オブジェクトはデータとともに変化しています(これは、readメソッドではinitialDataConfiguration
データ関数のスコープを取得しているがあります。
スコープを継承せずに初期構成データを取得する方法はありますか?
私はこれを考えすぎていますか?これを行うためのより良い/より簡単な方法がありますか?
初期データをハードコーディングすることが唯一のオプションですか?