0

我有以下代码片段:

App.TripLegView = Em.View.extend({
     transportMeanType: null,
     transportMeanTypeChanged: function  () {
          this.carView = App.CarView.create();
          var childView = this.createChildView(this.carView );
          this.get('childViews').pushObject(childView);
     }
}).observes('transportMeanType'),

App.CarView = Em.View.extend();

但是,当我调试上面的代码时,子视图似乎没有添加到 childViews 数组中。

谁能解释如何正确添加子视图。

4

1 回答 1

0

首先,您似乎在滥用observes(),它应该在函数上定义。如果你想操作子视图,我认为你应该使用Ember.ContainerView

您的代码将是这样的:

App.CarView = Em.View.extend();

App.TripLegView = Em.ContainerView.extend({
  transportMeanType: null,

  transportMeanTypeChanged: function  () {
    this.carView = App.CarView.create();
    var childView = this.createChildView(this.carView );
    this.get('childViews').pushObject(childView);
  }.observes('transportMeanType')
});

如果它不起作用,请发布一个 jsfiddle 来举例。

于 2012-08-14T19:10:11.427 回答