状況
このような状況を想定しています。
- <input type="text">年というパーツと、<button>うるう年</button>というパーツがある。
- 決定ボタンは一つ上の要素が管理している(ほかにも月や日、曜日といったパーツがあり共通化したかった)
- <input>が編集されたら、逐一上の要素へ変更を通知したい(そうしないと決定ボタンが押されたとき困る)
- 決定がおされるか、うるう年が押されたらアラートを出したい。
- アラートは 「XX年」あるいは「うるう年」と表示させたい。
コード
とりあえずコードみてください。JavaScriptで Riot + RiotControlという構成です。
html
1<my-form></my-form>
javascript
1<my-form> 2 <my-editor></my-editor><button onclick={finish}>決定</button><br> 3 RESULT: {value} 4 <script> 5 this.on('mount', _ => riot.mount('my-editor', 'my-year-editor')); 6 const action = new class Action { 7 finish() {RiotControl.trigger('finish');} 8 }; 9 const store = new class Store { 10 constructor() { 11 riot.observable(this); 12 this.value = ''; 13 this.actionTypes = { 14 changed: 'store-changed' 15 }; 16 this.on('edit', text => { 17 this.value = text; 18 this.trigger(this.actionTypes.changed); 19 }); 20 this.on('finish', _ => alert(this.value)); 21 } 22 } 23 this.finish = _ => { 24 action.finish(); 25 } 26 this.value = store.value; 27 RiotControl.addStore(store); 28 store.on(store.actionTypes.changed, _ => { 29 this.value = store.value; 30 this.update(); 31 }); 32</my-form>
javascript
1<my-year-editor> 2 <input type="text" onkeyup={edit}>年 <button onclick={leap}>うるう年</button> 3 <script> 4 const action = new class Action{ 5 edit(text) { 6 RiotControl.trigger('edit', text); 7 } 8 finish() { 9 RiotControl.trigger('finish'); 10 } 11 }; 12 this.edit = e => action.edit(e.target.value + '年'); 13 this.leap = _ => { 14 // 以下が疑問のコード。editとfinishを発行している 15 action.edit('うるう年'); // <my-form>内のstoreの値を変更する必要がある 16 action.finish(); // うるう年ボタンは決定ボタンの役割も兼ねる 17 } 18</my-editor>
長文になってしまいすみません…回答していただけると嬉しいです
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/06 08:04
2016/08/06 08:10
2016/08/06 08:13