簡體   English   中英

如何動態地將觀察者方法添加到Ember.js對象

[英]how to dynamically add observer methods to an Ember.js object

所以我試圖動態地將這些觀察者方法添加到Ember.js對象

holderStandoutCheckedChanged: (->
    if @get("controller.parent.isLoaded")
        @get("controller").toggleParentStandout(@get("standoutHolderChecked"))
).observes("standoutHolderChecked")

holderPaddingCheckedChanged: (->
    if @get("controller.parent.isLoaded")
        @get("controller").toggleParentPadding(@get("holderPaddingChecked"))
).observes("holderPaddingChecked")

holderMarginCheckedChanged: (->
    if @get("controller.parent.isLoaded")
        @get("controller").toggleParentMargin(@get("holderMarginChecked"))
).observes("holderMarginChecked")

到目前為止我有這個代碼但是沒有調用item.methodToCall函數

methodsToDefine = [
    {checkerName: "standoutHolderChecked", methodToCall: "toggleParentStandout"},
    {checkerName: "holderPaddingChecked", methodToCall: "toggleParentPadding"},
    {checkerName: "holderMarginChecked", methodToCall: "toggleParentMargin"}
]

add_this = { }

for item in methodsToDefine
    add_this["#{item.checkerName}Changed"] = (->
        if @get("controller.parent.isLoaded")
            @get("controller")[item.methodToCall](@get(item.checkerName))
    ).observes(item.checkerName)

App.ColumnSetupView.reopen add_this

誰能告訴我我做錯了什么? 有一個更好的方法嗎 ? 我應該在混音中這樣做嗎? 如果是的話請

我不知道你確切的用例,但正如你所說,你所描述的問題可以通過Mixin解決,請參閱http://jsfiddle.net/pangratz666/a3Usx/

JavaScript

App = Ember.Application.create();

var methodsToDefine = [
    {checkerName: "standoutHolderChecked", methodToCall: "toggleParentStandout"},
    {checkerName: "holderPaddingChecked", methodToCall: "toggleParentPadding"},
    {checkerName: "holderMarginChecked", methodToCall: "toggleParentMargin"}
];

App.Stalker = Ember.Mixin.create({
  init: function() {
    this._super();
    methodsToDefine.forEach(function(config) {
      // add an observer for checkerName - a change should call methodToCall
      Ember.addObserver(this, config.checkerName, this, config.methodToCall);
    }, this);
  },

  willDestroy: function() {
    this._super();

    // since we are good citizens, we remove the observers when the object is destroyed
    methodsToDefine.forEach(function(config) {
      Ember.removeObserver(this, config.checkerName, this, config.methodToCall);
    }, this);
  }
});

示例用例:

var myObj = Ember.Object.create(App.Stalker, {
  toggleParentStandout: function() {
    console.log("toggleParentStandout called");
  },
  toggleParentPadding: function() {
    console.log("toggleParentPadding called");
  },
  toggleParentMargin: function() {
    console.log("toggleParentMargin called");
  }
});

myObj.set('standoutHolderChecked', 42);
myObj.set('holderPaddingChecked', 'Buster');

另一個實現是mixin,它使用一個數組watchProperties ,這是一個應該觀察的屬性列表,參見http://jsfiddle.net/pangratz666/bSF3Z/

JavaScript

App = Em.Application.create();

App.Stalker = Ember.Mixin.create({
  init: function() {
    this._super();

    var props = this.get('watchProperties');
    Ember.assert("watchProperties should be an array", Ember.isArray(props));
    props.forEach(function(property) {
      // invoke <property>Changed when <property> changes ...
      Ember.addObserver(this, property, this, '%@Changed'.fmt(property));
    }, this);
  },

  willDestroy: function() {
    this._super();

    this.get('watchProperties').forEach(function(property) {
      Ember.removeObserver(this, property, this, '%@Changed'.fmt(property));
    }, this);
  }
});

var o = Ember.Object.create(App.Stalker, {
  // 'a b'.w() == ['a', 'b']
  watchProperties: 'a b'.w(),
  aChanged: function() {
    console.log("a changed");
  }
});

o.set('a', 123);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM