Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controllers not extending yieldTemplates #249

Closed
FredericoC opened this issue Oct 24, 2013 · 4 comments
Closed

Controllers not extending yieldTemplates #249

FredericoC opened this issue Oct 24, 2013 · 4 comments

Comments

@FredericoC
Copy link

I have the following (Coffescript):

Router.configure
  layoutTemplate: 'layout'
  notFoundTemplate: 'notFound'
  loadingTemplate: 'layoutLoading'

  yieldTemplates:
    layoutMasthead: {to: 'mastHeader'}
    uploadQueue: {to: 'uploadQueue'}
    layoutFooter: {to: 'footer'}
@GPRouteController = RouteController.extend
  customLoading: ->
    if @ready()
      NProgress.done()
    else
      NProgress.start()
      @stop()
ProjectController = GPRouteController.extend
  yieldTemplates:
    projectHeaderNav: {to: 'header'}

  projectExists: ->
    unless gp.Permission.canViewProject(Meteor.userId(), @params.projectId)
      @redirect('projects.forbidden')
      @stop()

  before: [
    ->
      @subscribe("project", @params.projectId).wait()
      @subscribe("project.externalShared.user.list", @params.projectId).wait()
    ->
      @customLoading()
    ->
      @projectExists()
    ->
      Session.set('projectId', @params.projectId)
  ]
  @route 'projects.plans',
    path: '/projects/:projectId/plans'
    template: 'plans'
    controller: ProjectPlansController

The "before" hooks extend perfectly, but the "yieldTemplates" property does not. If i use Coffescript native inheritance it will just throw and exception with "before" not defined (I can get the actual error if you want). If I define "projectHeaderNav: {to: 'header'}" in "Router.configure" it works fine.

Any ideas?

@th0r
Copy link

th0r commented Oct 24, 2013

@FredericoC, I think issue #150 is about the same. You can check out my comment there.

@cmather
Copy link
Contributor

cmather commented Oct 27, 2013

What's happening here is your Router level config is overriding the RouteController prototype property. It's a bit of a confusing paradigm and maybe we should change it.

In general, options override prototype properties in iron-router. For example:

Controller = function (options) {
  // if we got a value for yieldTemplates from options just use that, otherwise use whatever is currently attached to 'this'
  this.yieldTemplates = options.yieldTemplates || this.yieldTemplates;
};

Controller.prototype.yieldTemplates = {};

So when you globally configure properties on the Router, we pass those along as options to the RouteController. So it's not really inheriting, it's just being passed as an option which is completely overriding the prototype property.

It's possible allowing this behavior was a mistake :-). If you're using RouteControllers I would not use global router config at all (except for autoRender and autoStart etc). Instead, create a super controller and just inherit from that.

ApplicationController = RouteController.extend({
  yieldTemplates: {}
});

ChildController = ApplicationController.extend({
  // inherits yield templates
});

@cmather cmather closed this as completed Oct 27, 2013
@wbashir
Copy link

wbashir commented Jun 14, 2014

@cmather Is this still true with how the RouteController works. I tried creating as you mentioned in you're last example, a super controller but my data option is being overriden:

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

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

@wbashir
Copy link

wbashir commented Sep 7, 2014

@cmather Still having this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants