【问题标题】:Any method for accessing parent object properties in Ember?在 Ember 中访问父对象属性的任何方法?
【发布时间】:2012-03-15 15:47:37
【问题描述】:

This question 展示了如何获取父视图的属性。

有没有办法实现与一般Ember.Objects 相同的效果。比如child如何访问App.Parent的属性:

App.Parent = Ember.Object.extend({
  parentProperty: 'someValue',

  child: App.Child.create()  

});

App.Child = Ember.Object.extend({
   init: function(){
     // I don't know which is my parent object
     // but I still want to access the value of `parentProperty`
     // var parentProperty = ???

   }
});

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    我整理了一个 jsfiddle,演示了如何使用“父”属性更新子对象。基本思想是在创建父项时检查子项的存在,并观察子项是否发生变化。

    http://jsfiddle.net/KqCXr/6/

    希望它会有所帮助。在这里评论是您有任何问题。

    【讨论】:

      【解决方案2】:

      你可以在创建孩子的时候传入父母吗?

      App.Parent = Ember.Object.extend({
        parentProperty: 'someValue',
      
        child: App.Child.create({ parent: App.Parent })  
      })
      

      【讨论】:

      • 不能总是这样做,尤其是如果父级是数组中的一个实例。
      猜你喜欢
      • 2015-06-26
      • 2011-06-16
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多