繁体   English   中英

无法从外部访问ember-pikaday onSelection中的日期

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM