Ember.js 如何将静态数据属性指定给余烬视图?

Ember.js 如何将静态数据属性指定给余烬视图?,ember.js,custom-data-attribute,Ember.js,Custom Data Attribute,我需要为一个Ember.View分配一个静态数据属性,如何在View对象中而不是在{{View}标记中设置它 App.MessagesFormView = Ember.View.extend({ tagName: 'div', classNames: ['modal', 'fade'], didInsertElement: function() { this.$().modal('show') }, willDestroyElement: function() {

我需要为一个Ember.View分配一个静态数据属性,如何在View对象中而不是在
{{View}
标记中设置它

App.MessagesFormView = Ember.View.extend({
  tagName: 'div',
  classNames: ['modal', 'fade'],
  didInsertElement: function() {
    this.$().modal('show')
  },
  willDestroyElement: function() {
    this.$().modal('hide')
  },
})

这必须使用Ember.View对象的
属性bindings
数据背景
数据属性来完成

App.MessagesFormView = Ember.View.extend({
  tagName: 'div',
  classNames: ['modal', 'fade'],
  // Set a data attribute, of a view, requires both an attribute binding and
  // an attribute assignment
  attributeBindings: ['data-backdrop'],
  'data-backdrop': 'static',
  didInsertElement: function() {
    this.$().modal('show')
  },
  willDestroyElement: function() {
    this.$().modal('hide')
  },
})

不幸的是,我没有足够的声誉来评论Ola的答案,但我相信更好的方法是不使用字符串(引号中的文本)来表示数据属性名称。相反,在camelCase中写入您的属性名称,Ember将自动将其绑定到连字符的attributebinding。例如:

App.MessagesFormView = Ember.View.extend({
  tagName: 'div',
  attributeBindings: ['data-backdrop'], 
  dataBackdrop: 'static', // Binds to data-backdrop. Awesome!
});

我希望这是有道理的

您的示例确实做到了这一点,但所做的不仅仅是向元素添加一个简单的静态属性。它将属性绑定到属性值,并随后将属性更改传播到属性,这对于某些用例(引导
data toggle
any?)来说可能是一种过度杀伤力。我怀疑添加真正静态属性的唯一方法是通过
diInsertElement
中的
this.$().attr('data-toggle','collapse')
。出于某种原因,这对我不起作用。我同意这看起来像是Ember的方式,但没有这样做(我是在一个组件中添加属性,而不是视图,可能会改变什么?)可能是Ember的更新删除了这个功能?@DuncanWalker你使用的是哪个版本的Ember?现在是1.9.1,但我不确定最初回答问题时使用的版本。