0

你们中有人使用 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' ]
4

1 回答 1

1

使用 helper 的主要好处之一action是它会自动this为您准备上下文;当你使用它时。例如,如果您在onSelection没有动作助手的情况下传递函数,{{pikaday-inputless onSelection=onSelection}}那么“this context”将是pikaday-inputless处理函数中的组件,而不是您自己的组件(父组件)onSelection。这就是 kumkanillam 在此评论中已经提到的内容。因此,您需要使用pikaday-inputless onSelection=(action 'onSelection')“此上下文”作为您自己的组件。

我为你准备了一个非常简单的twiddle来说明使用 action helper 可以顺利进行。您只需要将您的操作处理程序放在actions组件的 json 声明中。my-component在旋转中查看。它只是做了我上面已经总结的事情。希望这可以帮助。

于 2017-07-25T10:04:17.270 回答