Inheritance 路由控制器上的IronRouter扩展数据选项

Inheritance 路由控制器上的IronRouter扩展数据选项,inheritance,meteor,iron-router,Inheritance,Meteor,Iron Router,当使用IronRouter和RouteControl时,是否有方法扩展数据选项,当我从超级控制器继承时,它似乎被覆盖,子控制器不扩展定义的数据属性。我在路由上的yieldTemplates选项也遇到类似问题,并使用了变通方法(下划线扩展),但在这种情况下不起作用: ApplicationController = RouteController.extend({ data: function(){ return { user: Met

当使用IronRouter和
RouteControl
时,是否有方法扩展
数据
选项,当我从超级控制器继承时,它似乎被覆盖,子控制器不扩展定义的
数据
属性。我在路由上的
yieldTemplates
选项也遇到类似问题,并使用了变通方法(下划线扩展),但在这种情况下不起作用:

ApplicationController = RouteController.extend({
     data: function(){
          return {
                 user: Meteor.user()   
         }     
   }
});

ChildController = ApplicationController.extend({
  data: function(){
        return {
               // I expect to inherit Meteor.User ?????
               someData: {}
        }
   }
});
编辑:

在使用
下划线
扩展
函数继承原型函数后,我仍然无法在
路由
中继承使用
儿童控制器的定义

this.route('someRoute', {
   template: 'task_template',
   //tasks is not available on the template
   data: function () {
            var base = ChildController.data.call(this);
            console.log(base);
            return _.extend(base, {
                tasks: Tasks.find({state: 'Open'})
            });
});

我认为
.extends
在这种情况下也应该起作用:

ChildController = ApplicationController.extend({
  data: function() {
    var base = ApplicationController.data.call(this);
    return _.extends(base, {
      someData: {},
    });
  }
});

我在生产应用程序中使用了类似的东西:

Router.route('/test/:testparam', {
    name: 'test',
    controller: 'ChildController'
});

ParentController = RouteController.extend({
    data: function() {
        console.log('child params: ', this.params);
        return {
            foo: 'bar'
        };
    }
});

ChildController = ParentController.extend({
    data: function() {
        var data = ChildController.__super__.data.call(this);
        console.log(data);
        return data;
    }
});
使用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

您可以使用u.extend来扩展数据()

来添加到所选答案,请注意,如果您使用的是Coffeescript,则以下操作将产生相同的结果:

class @ChildController extends ParentController
  data: -> super()
  # or if you want to add to it:
  data: ->
    _.extend super(), { extraThing: 'some value' }

另一个可能实现相同结果的选项是在父控制器上定义一个方法,然后使用super调用它,而不扩展任何内容。每个控制器的工作量稍大一些,但更容易追溯应用。此外,它使该方法对于您的子控制器是可选的,而不是默认情况下

ApplicationController = RouteController.extend({
        waitOn: function() {
           return [Meteor.subscribe('customUserPublish')];
        },
        GetProfileWithEmail: function(){
           var user = Meteor.user();
           var profile = user.profile;
           profile.email = user.emails[0].address;
           return profile;
        }
    });

ProfileController = ApplicationController.extend({
        waitOn: function() {
          return [Meteor.subscribe('anotherCollectionPublish')];
        },
        data: function(){
          return {
            profile:  function(){
              var profile = ApplicationController.__super__.GetProfileWithEmail.call(this);
              console.log('profile from super', profile);
              return profile;
            }
          }
        }
});

请记住,您还必须订阅已发布的集合,并且我相信您需要使用waitOn数组选项,以便它能够正确地合并sub(无可否认,我总是使用数组格式,以便使用YMMV)。您可以使用{{with profile}}…{/with}}访问模板中的数据,或者如果要返回对象数组,请使用{{each profile}…{/each}。

扩展
还是
扩展
,我尝试了这两种方法,但仍然无法使用
ChildController
中的数据。如果我从
应用程序控制器
中删除
数据
定义,它就可以工作了。您能解释一下为什么要这样做,以及您试图实现的目标吗?也许还有另一种方法?您还需要从child的data()调用父(超级)控制器。真不知道为什么人们不投票给4U。这是一个很棒的解决方案。谢谢!这可以在不扩展控制器的情况下完成吗?通过直接在
此路径上使用
定义?