1

My view is written as follows:

<ul class="commentslist cf">
        <li class="cf" ng-repeat="(key,comment) in activity.comments">
          <div class="comment">{{comment.name}}
            <div class="buttons" ng-show="isPostedUser(activity.$id, key, currentUser)">
              <button class="btn btn-delete tooltip"
                confirmation-needed = "Are you sure you want to delete this activity?"
                ng-click="deleteComment(activity.$id,key)">
                <span>Delete this comment</span></button>
            </div><!-- buttons -->
          </div><!-- comment -->
        </li>            
</ul>

In my controller associated with this view, there is a function called:

$scope.isPostedUser = function(actId, key, user) {
var refComment = new Firebase(FIREBASE_URL + "users/" + $scope.whichuser + "/activities/" + actId +
  "/comments/" + key);
var commentObj = $firebase(refComment).$asObject();
commentObj.$bindTo($scope, "data").then(function() {
  return $scope.data.giver === user.$id;
});

};

The purpose of this function is to display the delete button only isPostedUser evaluates to true. I tested and it does evaluate to true, but it still does not display the button. Any idea why?

abc
  • 31
  • 4

2 Answers2

3

Let's rewrite your function with a proper indentation:

$scope.isPostedUser = function(actId, key, user) {
    var refComment = new Firebase(FIREBASE_URL + "users/" + $scope.whichuser + "/activities/" + actId + "/comments/" + key);
    var commentObj = $firebase(refComment).$asObject();
    commentObj.$bindTo($scope, "data").then(function() {
        return $scope.data.giver === user.$id;
    });
};

Now, you can see that your function doesn't return anything (which means it returns undefined, which is falsy).

The return statement that the code contains returns from the callback passed to the then() function. This statement is executed asynchronously, after isPostedUser() has returned.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks a lot. Btw, sorry for not indenting the code! – abc Apr 25 '15 at 19:46
  • 1
    My point was that the lack of indentation hurts you, in the first place. With proper indentation, the structure of the code becomes obvious, and you can read and understand your own code much easier. – JB Nizet Apr 25 '15 at 19:48
0

So I fixed it as follows:

$scope.isPostedUser = function(actId, key, user) {
    var refComment = new Firebase(FIREBASE_URL + "users/" + $scope.whichuser + "/activities/" + actId + "/comments/" + key);
    var commentObj = $firebase(refComment).$asObject();
    commentObj.$bindTo($scope, "data");
    return $scope.data.giver === user.$id;
};

If anyone has any better solution, please let me know.

abc
  • 31
  • 4