簡體   English   中英

如何為ember視圖分配靜態數據屬性?

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

我需要為Ember.View分配一個靜態數據屬性,如何在View對象而不是{{view }}標簽中設置它。

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

不幸的是,我沒有足夠的聲譽來評論Ola的答案,但我認為更好的方法是不使用字符串(引號中的文本)來表示數據屬性屬性名稱。 相反,在camelCase中編寫屬性名稱,Ember會自動將其綁定到帶連字符的屬性綁定。 例如:

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

我希望這是有道理的!

這必須使用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