IronRouter在路由控制器上扩展数据选项 [英] IronRouter extending data option on route controller

查看:77
本文介绍了IronRouter在路由控制器上扩展数据选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用IronRouter和 RouteController 时,有没有办法扩展数据选项,好像它得到了当我从超级控制器继承时,子控制器不会扩展已定义的数据属性。我在路由上遇到了与 yieldTemplates 选项类似的问题,并使用了一种解决方法(下划线_extends),但在这种情况下它不起作用:

Is there a way to extend the data option when using IronRouter and the RouteController, It seems like it gets overridden when I inherit from a super controller, the child controller doesn't extend the defined data properties. I have had similiar issues with the yieldTemplates option on a route and used a workaround (underscore _extends) but it didn't work in this case:

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

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

编辑:

使用<$后c $ c>下划线和 extend 函数继承原型函数,我仍无法继承 route 定义使用 ChildController

After using underscore and the extend function to inherit the prototype function, I am still unable to inherit in route definition's that use the ChildController

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'})
            });
});


推荐答案

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

I use something similar to this in a production app:

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;
    }
});

使用 __ super __ 似乎可以解决问题!

您可以使用_.extend扩展数据( http://underscorejs.org/#延伸

Using __super__ seems to do the trick!
You can than use _.extend to extend the data (http://underscorejs.org/#extend)

这篇关于IronRouter在路由控制器上扩展数据选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆