我是聚合物的新手,想要從其父元素內的元素獲取返回值。聚合物從元素中獲取返回值並使用實例方法
基本上,單擊按鈕時,我想加載一個對話框,其中包含單選按鈕,當對話框中單擊確認按鈕時,將返回選中的單選按鈕的值。但是,每次打開對話框時,我都想清除單選按鈕並使用ajax調用重新加載列表。
我的問題是1,我似乎無法使用聚合物對象內的任何成員函數。其次,我不認爲我正在從對話框元素中正確獲取「返回」值。
這裏是我的父元素:
<dom-module id="opr-remote">
<template>
<paper-button raised on-tap="onTap" id="play-file">Play file</paper-button>
<opr-play-file-dialog id="playFileDialogElement"></opr-play-file-dialog>
</template>
<script>
Polymer({
is: 'opr-remote',
onTap: (e) => {
const id = e.target.getAttribute('id');
if ('play-file' === id) {
this.playFileDialogElement.open((playPosition) => {
console.log('play position: ' + playPosition);
});
}
},
});
</script>
</dom-module>
這將打開對話框罰款,但這裏是我的對話框元素:
<dom-module id="opr-play-file-dialog">
<template>
<iron-ajax
id="currentPlaylistAjax"
url="/current-playlist"
handle-as="json"
last-response="{{ajaxResponse}}"
on-response="handleResponse">
</iron-ajax>
<paper-dialog
entry-animation="scale-up-animation"
exit-animation="fade-out-animation"
id="playFileDialog">
<h2>Play file</h2>
<paper-spinner active id="playFileLoadingSpinner"></paper-spinner>
<paper-radio-group on-change="positionOnChange" id="positionRadio" attr-for-selected="value">
<template is="dom-repeat" items="[[ajaxResponse.data]]">
<paper-radio-button value="[[item.position]]">[[item.fileName]]</paper-radio-button>
<br />
</template>
</paper-radio-group>
<div class="buttons">
<paper-button dialog-dismiss>Cancel</paper-button>
<paper-button dialog-confirm autofocus disabled id="playButton" on-tap="playPressed">Play</paper-button>
</div>
</paper-dialog>
</template>
<script>
Polymer({
is: 'opr-play-file-dialog',
_selectedPosition: -1,
_playPositionCallback: null,
clearSelectedPosition:() => {
this.positionRadio.select(-1);
this._selectedPosition = -1;
this.playButton.disabled = true;
},
open: (playPositionCallback) => {
this._playPositionCallback = playPositionCallback;
console.log(this._selectedPosition);// is undefined
// if this is un-commented, throws an error as it not being a function
//this.clearSelectedPosition();
this.positionRadio.hidden = true;
this.playFileLoadingSpinner.hidden = false;
this.playFileDialog.open();
this.currentPlaylistAjax.generateRequest();
},
handleResponse:() => {
//this.clearSelectedPosition();
this.positionRadio.hidden = false;
this.playFileLoadingSpinner.hidden = true;
},
positionOnChange: (e) => {
const target = e.target;
this._selectedPosition = target.value;
this.playButton.disabled = false;
},
playPressed: (e) => {
if (this._selectedPosition < 1) {
return;
}
this._playPositionCallback(this._selectedPosition);
},
});
</script>
</dom-module>
正如你看到的,我不能叫clearSelectedPosition
。然後,我使用回調來獲得位置,但我不認爲這是正確的方法。