【问题标题】:Can't access date in ember-pikaday onSelection from the outside无法从外部访问 ember-pikaday onSelection 中的日期
【发布时间】:2017-12-29 20:28:21
【问题描述】:

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

【问题讨论】:

  • 如何将其包含在 hbs 中?从 addon 中阅读我,{{pikaday-input onSelection=(action 'setSelectedDate')}},您可以在组件中定义 setSelectedDate。
  • 请更新有问题的,对其他人发现问题有用
  • @kumkanillam 是的,我已经更新了问题。
  • 试试pikaday-inputless onSelection=(action onSelection)
  • 它给了我TypeError: Cannot read property 'setAttribute' of undefined 将onSelection 放在actions: {} 下的错误

标签: javascript ember.js ember-components pikaday


【解决方案1】:

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

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

【讨论】:

  • 您知道是否可以在选择器中保留所选日期?
  • 据我所知;你不能这样做; pikaday 依赖于动作;您不能直接绑定到选定的日期值;但这又是我所知道的;我可能错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 2021-04-12
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多