javascript - 将 bootstrap-tags 包装到 Ember 组件中

标签 javascript twitter-bootstrap ember.js

所以,我正在使用 https://github.com/maxwells/bootstrap-tags 中的 Bootstrap 标签。 我正在尝试将标签实现到 Ember 组件中,目前如下所示:

import Ember from 'ember';

export default Ember.Component.extend({
  tagName: 'input',
  classNames: null,
  value: null,
  initTagsInput() {
    const $el = this.$();
    const _this = this;
    $el.tag({
      placeholder: 'enter associated plasmids',
      beforeAddingTag: function(tag){          //here is my problem I think?
        this.set('value', tag);
      }
    })
  },

  didInsertElement() {
    this._super();
    this.initTagsInput();
  }
});

我遇到的问题是尝试设置该值,但是当我检查控制台或 ember 调试器时,没有分配任何值(值仍然为空)。就像 beforeAddingTag 永远不起作用一样!我对 ember 还很陌生,所以任何关于包装这个库的清晰说明都会有所帮助。

我需要在某处使用可观察对象吗?

最佳答案

我想你想要:

this.set('value', tag);

成为:

_this.set('value', tag);

还有

this.$().tag(/**/)

成为:

this.$().tags(/**/)

该库的示例也使用 div,因此我们可以删除:

tagName: 'input'

占位符在此库中的添加方式也有所不同:

placeholder: 'enter associated plasmids'

应该是:

placeholderText: 'enter associated plasmids'

也就是说,我们可以使用 ES6 中的粗箭头使 this 引用外部上下文,并使其更漂亮:

import Ember from 'ember';

export default Ember.Component.extend({
  classNames: null,
  value: null,
  initTagsInput() {
    this.$().tags({
      placeholderText: 'enter associated plasmids',
      beforeAddingTag: tag => {
        this.set('value', tag);
      }
    })
  },

  didInsertElement() {
    this._super();
    this.initTagsInput();
  }
});

关于javascript - 将 bootstrap-tags 包装到 Ember 组件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31862257/

相关文章:

javascript - 即使它是全局变量,也无法比较类内部变量的值

javascript - 如何使用javascript比较多个数组在每个数组中仅同一时间获取结果?

javascript - 使用 jQuery 对同级 <div> 进行排序

javascript - 单击时如何在 Bootstrap 中分别为导航的白色和黑色添加背景和文本颜色?

ember.js - Ember : Build error after installing ember-table

javascript - redux - 当一个资源依赖于另一个资源时,如何适本地更新它们

javascript - 延迟关闭 Bootstrap 下拉菜单

html - 为什么 Bootstrap Span 类没有出现

ember.js - 测试时如何从组件中获取属性?

ember.js - Ember Cli - 在 ember-cli-build 中转换供应商 ES6 依赖?