1

I'm using parse with angularjs to authenticate users. Here is the login function.

$scope.doLogin = ->
    Parse.User.logIn $scope.currentUser.username, $scope.currentUser.password,
      success: (user) ->
        console.log user
        $scope.currentUser = user
      error: (user, error) ->
        console.log error

And here is the form (used twice in same page, navbar dropdown and in page content):

%form{"ng-submit" => "doLogin()"}
  %input{"ng-model" => "currentUser.username", type: "text"}
  %input{"ng-model" => "currentUser.password", type: "password"}

  %button.btn.btn-block
    %center Connexion

The problem is that whenever the form is submitted, I can see the user object in console, but $scope.currentUser doesn't always get updated. Sometimes I have to submit the form 3 or 4 times in a row for it to get updated.

What am I doing wrong ? Thank you.

4
  • 1
    isn't it just because it's async? Commented Jul 5, 2013 at 16:35
  • @user1737909 I know but how to solve this issue ? Commented Jul 5, 2013 at 16:53
  • You can call .then on the promise object Commented Jul 5, 2013 at 20:21
  • stackoverflow.com/a/20842759/429521 Commented Dec 30, 2013 at 16:00

1 Answer 1

1

perhaps you should include $scope.apply() in your success callback. From the Angularjs docs:

"$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life-cycle of exception handling, executing watches."

I had a similar problem with Parse and I'd solved it with $scope.apply().

Here is my controller

ps. note that I'm relatively new to angularjs and the above code may not be the most efficient :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.