IronRouter extending data option on route controller

前端 未结 4 1645
清酒与你
清酒与你 2021-02-20 13:16

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 co

4条回答
  •  天命终不由人
    2021-02-20 13:32

    Another option that may achieve the same result is to define a method on your parent controller, then call it using super without extending anything. It's slightly more work for each controller but is easier to apply retroactively. Also it makes the method optional for your child controller rather than by default.

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

    Remember that you have to subscribe to the published collection as well and I believe that you need to use the waitOn array option so that it will merge the subs properly (admittedly I always use an array format so YMMV). You can access the data in your template using {{#with profile}}...{{/with}} or if you were getting back an array of objects use {{#each profile}}...{{/each}}.

提交回复
热议问题