[英]Can't access date in ember-pikaday onSelection from the outside
你們有沒有使用ember-pikaday插件體驗過這一點? 我想將onSelection
的日期設置為存儲在selectedDate
上,但是onSelection內部的上下文是插件的,而不是組件本身的。 有沒有辦法將date
的值存儲到組件中的一個屬性?
我的代碼是這樣的:
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['schedule-nav-date-picker'],
selectedDate: null,
onSelection: function(date) {
this.set('selectedDate', date);
return date;
},
actions: {
saveClicked () {
this.sendAction('saveClicked', this.get('selectedDate'));
},
cancelClicked () {
this.sendAction('cancelClicked');
}
}
});
它給了我這個錯誤:
pikaday.js:165 Uncaught TypeError: this.get(...) is not a function
at Class.userSelectedDate (pikaday.js:165)
at Class.onPikadaySelect (pikaday.js:151)
at Backburner.run (ember.debug.js:224)
at Backburner.join (ember.debug.js:247)
at Function.run.join (ember.debug.js:15904)
at Pikaday.<anonymous> (ember.debug.js:15966)
at Pikaday.setDate (pikaday.js:815)
at HTMLDivElement.Pikaday.self._onMouseDown (pikaday.js:462)
這是模板,(我們使用Emblem進行模板制作):
h1 Select a Date
.form-groups-container.-include-container
.form-section
.form-group
.row
pikaday-inputless onSelection=onSelection
form-buttons [
saveLabel='Save'
cancelLabel='Cancel'
saveClicked='saveClicked'
cancelClicked='cancelClicked' ]
使用action
助手的主要好處之一是它會自動為您准備this
上下文。 當您使用它時。 例如,如果您傳遞的onSelection
函數沒有操作輔助程序,例如{{pikaday-inputless onSelection=onSelection}}
則“此上下文”將是pikaday-inputless
組件,而不是處理函數onSelection
您自己的組件(父組件)。 這是此評論中已經提到的kumkanillam。 因此,您需要對“此上下文”使用pikaday-inputless onSelection=(action 'onSelection')
作為自己的組件。
我已經准備了一個非常簡單的玩弄 ,為您說明使用動作助手工程進展順利。 您只需要將動作處理程序放入組件的actions
json聲明中即可。 在旋轉中查看my-component
。 它只是做了我上面已經總結的事情。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.