简体   繁体   English

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

[英]Can't access date in ember-pikaday onSelection from the outside

Has any of you experienced this using the ember-pikaday addon? 你们有没有使用ember-pikaday插件体验过这一点? I would like to set the date from the onSelection to be stored on the selectedDate but the context inside the onSelection is of the addon and not of the component itself. 我想将onSelection的日期设置为存储在selectedDate上,但是onSelection内部的上下文是插件的,而不是组件本身的。 Is there a way I could store the value of date to one of the properties in my component? 有没有办法将date的值存储到组件中的一个属性?

My code is like this: 我的代码是这样的:

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');
    }

  }
});

And it gives me this error: 它给了我这个错误:

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)

And this is the template, (we use Emblem for templating): 这是模板,(我们使用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' ]

One of the main benefits of using action helper is that it automatically prepares this context for you; 使用action助手的主要好处之一是它会自动为您准备this上下文。 when you use it. 当您使用它时。 For instance if you pass the function onSelection without the action helper like {{pikaday-inputless onSelection=onSelection}} then "this context" will be the pikaday-inputless component not your own component (parent component) within the handler function onSelection . 例如,如果您传递的onSelection函数没有操作辅助程序,例如{{pikaday-inputless onSelection=onSelection}}则“此上下文”将是pikaday-inputless组件,而不是处理函数onSelection您自己的组件(父组件)。 This is what kumkanillam already mentioned in this comment. 这是此评论中已经提到的kumkanillam。 So you need to use pikaday-inputless onSelection=(action 'onSelection') for "this context" to be your own component. 因此,您需要对“此上下文”使用pikaday-inputless onSelection=(action 'onSelection')作为自己的组件。

I have prepared a very simple twiddle for you to illustrate that using action helper works smoothly. 我已经准备了一个非常简单的玩弄 ,为您说明使用动作助手工程进展顺利。 You just need to put your action handler within actions json declaration of your component. 您只需要将动作处理程序放入组件的actions json声明中即可。 Check out my-component in the twiddle. 在旋转中查看my-component It simply does what I already summarized above. 它只是做了我上面已经总结的事情。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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