简体   繁体   English

如何为ember视图分配静态数据属性?

[英]How do you assign a static data- attribute to an ember view?

I need to assign a static data- attribute to an Ember.View, how do I set that in the View object instead of in the {{view }} tag. 我需要为Ember.View分配一个静态数据属性,如何在View对象而不是{{view }}标签中设置它。

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

Unfortunately, I don't have enough reputation to comment on Ola's answer, but I believe a slightly better way to do this is to not use a string (text in quotation marks) to denote the data attribute property name. 不幸的是,我没有足够的声誉来评论Ola的答案,但我认为更好的方法是不使用字符串(引号中的文本)来表示数据属性属性名称。 Instead, write your property name in camelCase and Ember will automatically bind it to the hyphenated attributebinding. 相反,在camelCase中编写属性名称,Ember会自动将其绑定到带连字符的属性绑定。 For example: 例如:

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

I hope that makes sense! 我希望这是有道理的!

This has to be done using both an attributeBindings and data-backdrop or data-whatever property of the Ember.View object. 这必须使用attributeBindings绑定和data-backdropdata-whatever Ember.View对象data-whatever属性。

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

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

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