olanod / ng-meteor

AngularJs with Meteor integration
20 stars 1 forks source link

Show current user #1

Open Polyrhythm opened 11 years ago

Polyrhythm commented 11 years ago

Can't get this to work, seems there is some kind of bug between Blade and this package.

Blade correctly shows the current user when Angular is disabled but Angular's load order with Blade seems to mess everything up.

See bminer/node-blade#173

Polyrhythm commented 11 years ago

Basically, the problem is that Angular loads before the Blade template can accept Meteor Template injections. If you bootstrap the Angular framework after Meteor startup, it happens after Blade automatically loads itself with Meteor and you end up getting two version of the DOM.

It's a chicken/egg situation where if you need to check reactive data like Meteor.user() with Angular, it needs to load the directive or service or controller checking it after Meteor starts up, but by loading Angular after Meteor starts, Meteor has already loaded a Blade template into the DOM and then Angular regenerates it.

Polyrhythm commented 11 years ago

I've developed a super hacky workaround, sort of.

angular.module('ngMeteor').controller 'HeaderCtrl', ($scope) ->
  Meteor.startup ->
    cb = ->
      $scope.loggedIn = ->
        if Meteor.user() && ! Meteor.loggingIn()
          return true
        else
          return false
      $scope.user = Meteor.user().username

    Meteor.setTimeout cb, 250

I have a controller that will control whether the user controls are shown on the header. I wrap all the user logic inside a Meteor.startup function and furthermore have Meteor wait 250ms before checking for the existence of a user. It works, sort of. Obivously not ideal. Would really love your thoughts on a proper way to handle this.

Neftedollar commented 10 years ago

@Polyrhythm My solution:

Deps.autorun(function(){
            $scope.currentUser = Meteor.user();
            $scope.$apply();
        });