• <bdo id='uPCcz'></bdo><ul id='uPCcz'></ul>
  • <legend id='uPCcz'><style id='uPCcz'><dir id='uPCcz'><q id='uPCcz'></q></dir></style></legend>

      <tfoot id='uPCcz'></tfoot>

        <i id='uPCcz'><tr id='uPCcz'><dt id='uPCcz'><q id='uPCcz'><span id='uPCcz'><b id='uPCcz'><form id='uPCcz'><ins id='uPCcz'></ins><ul id='uPCcz'></ul><sub id='uPCcz'></sub></form><legend id='uPCcz'></legend><bdo id='uPCcz'><pre id='uPCcz'><center id='uPCcz'></center></pre></bdo></b><th id='uPCcz'></th></span></q></dt></tr></i><div id='uPCcz'><tfoot id='uPCcz'></tfoot><dl id='uPCcz'><fieldset id='uPCcz'></fieldset></dl></div>

        <small id='uPCcz'></small><noframes id='uPCcz'>

      1. 如何将观察者方法动态添加到 Ember.js 对象

        how to dynamically add observer methods to an Ember.js object(如何将观察者方法动态添加到 Ember.js 对象)
          <legend id='zTgSn'><style id='zTgSn'><dir id='zTgSn'><q id='zTgSn'></q></dir></style></legend>
        1. <i id='zTgSn'><tr id='zTgSn'><dt id='zTgSn'><q id='zTgSn'><span id='zTgSn'><b id='zTgSn'><form id='zTgSn'><ins id='zTgSn'></ins><ul id='zTgSn'></ul><sub id='zTgSn'></sub></form><legend id='zTgSn'></legend><bdo id='zTgSn'><pre id='zTgSn'><center id='zTgSn'></center></pre></bdo></b><th id='zTgSn'></th></span></q></dt></tr></i><div id='zTgSn'><tfoot id='zTgSn'></tfoot><dl id='zTgSn'><fieldset id='zTgSn'></fieldset></dl></div>
              <tbody id='zTgSn'></tbody>

                <bdo id='zTgSn'></bdo><ul id='zTgSn'></ul>

                <small id='zTgSn'></small><noframes id='zTgSn'>

                  <tfoot id='zTgSn'></tfoot>
                • 本文介绍了如何将观察者方法动态添加到 Ember.js 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我正在尝试将这些观察者方法动态添加到 Ember.js 对象中

                  So i am trying to dynamically add these observer methods to a Ember.js object

                  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 函数没有被调用

                  I have this code so far but the item.methodToCall function is not getting called

                  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 中这样做吗?如果有请

                  Can anyone tell me what i am doing wrong ? Is there a better way to do this ? Should i be doing this in a mixin ? If so please

                  推荐答案

                  我不知道你的具体用例,但正如你所说,你描述的问题可以用 Mixin 解决,见 http://jsfiddle.net/pangratz666/a3Usx/

                  I don't know your exact use case, but as you said, your described problem could be solved with a Mixin, see 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');
                  

                  <小时>

                  另一种实现是使用数组 watchProperties 的 mixin,该数组是应观察的属性列表,请参阅 http://jsfiddle.net/pangratz666/bSF3Z/:


                  Another implementation would be a mixin which uses an array watchProperties, which is a list of properties which shall be observed, see 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);
                  

                  这篇关于如何将观察者方法动态添加到 Ember.js 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                  <small id='5bege'></small><noframes id='5bege'>

                    <tbody id='5bege'></tbody>
                  <tfoot id='5bege'></tfoot>
                • <legend id='5bege'><style id='5bege'><dir id='5bege'><q id='5bege'></q></dir></style></legend>
                          • <bdo id='5bege'></bdo><ul id='5bege'></ul>

                            <i id='5bege'><tr id='5bege'><dt id='5bege'><q id='5bege'><span id='5bege'><b id='5bege'><form id='5bege'><ins id='5bege'></ins><ul id='5bege'></ul><sub id='5bege'></sub></form><legend id='5bege'></legend><bdo id='5bege'><pre id='5bege'><center id='5bege'></center></pre></bdo></b><th id='5bege'></th></span></q></dt></tr></i><div id='5bege'><tfoot id='5bege'></tfoot><dl id='5bege'><fieldset id='5bege'></fieldset></dl></div>