你们中有谁在使用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' ]发布于 2017-07-25 18:04:17
使用action助手的主要好处之一是,当您使用它时,它会自动为您准备this上下文。例如,如果您在传递函数onSelection时没有使用{{pikaday-inputless onSelection=onSelection}}这样的操作帮助器,那么"this context“将是pikaday-inputless组件,而不是处理函数onSelection中您自己的组件(父组件)。这就是kumkanillam在这条评论中已经提到的。因此,您需要使用pikaday-inputless onSelection=(action 'onSelection')将"this context“作为您自己的组件。
我已经为你准备了一个非常简单的twiddle来说明使用action helper是如何流畅工作的。您只需要将您的操作处理程序放在组件的actions json声明中。看看my-component in the twiddle吧。它只是做了我在上面总结过的事情。希望这能有所帮助。
https://stackoverflow.com/questions/45277430
复制相似问题