简体   繁体   English

Ember.js:如何将组件属性传递给jQuery回调?

[英]Ember.js: How to pass component attribute to jQuery callback?

My component 我的组件

{{remind-me datetime=model.reminderDatetime comment=model.reminderComment}}

My remind-me.js: 我的hint-me.js:

import Ember from 'ember';

export default Ember.Component.extend({  

    picker: $(),

    didInsertElement() {
        this._super(...arguments);

        this.picker = $('.pickadate').pickadate({
                format: 'mmm dd, yyyy',
                onOpen: function() {

                    // how to get there this.get('datetime') ?           

                }
        });
    }
});

How to pass component attribute datetime to jQuery callback onOpen? 如何将组件属性datetime传递给jQuery回调onOpen? Thank you for answers. 谢谢你的回答。

You can use an arrow function instead, it will bind this to the surrounding scope, which is the component. 您可以改用箭头功能,它this功能绑定到周围的范围即组件。 Try something like: 尝试类似:

import Ember from 'ember';

export default Ember.Component.extend({  
  picker: $(),

  didInsertElement() {
    this._super(...arguments);

    this.picker = $('.pickadate').pickadate({
      format: 'mmm dd, yyyy',
      onOpen: () => {
        this.get('datetime');
      }
    });
  }
});

In addition to previous answer. 除了以前的答案。

1) You could use bind 1)您可以使用绑定

                onOpen: function() {

                    // how to get there this.get('datetime') ?           

                }.bind(this)

2) Or store "this" in a constant in function head where content "this" content hasn't changed yet. 2)或将“ this”存储在函数头中的常量中,其中内容“ this”的内容尚未更改。

    const self = this;
    ...
    onOpen: function() {

       self.get('datetime')          

   }

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

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