0

Situation:

I work with angular.js for frontend (newbie) and with symfony2 (php) for backend. I query for a list of users and groups. So the html could look like that.

<ul class="user-list">
    <li ng-repeat="user in users">
        <a ng:click="select(user)">
              {{user.first_name}} {{user.last_name}}
        </a>
    </li>
</ul>

<div class="user-was-selected" ng-show="selectedUser != null">
    <ul class="dropdown-menu">
        <li ng-repeat="group in groups"><a>{{ group.name }}</a></li>
    </ul>
</div>

When a user is selected from the list another view will be shown that displays a dropdown list filled with groups. This are all groups available. So the list query does not depend on the user.

However a user can be a member of a group. So I need to set a class on the li element, like "in-group".

In symfony2 this is easy, because its templating engine "twig" has methods like user.hasGroup to check if the user is in the group. I can do this right in the twig template.

My question is, do I have to make a rest api call for this? It would mean I have to make a rest api group check per group for every user. Does this make any sense or do you see another solution?

In addition to that, a click on a group from the dropdown would remove/add (toggle) the user from the clicked group.

1
  • You can use ng-class to conditionally apply a class. The condition would be something like "selectedUser.hasGroup". If you don't have that method, add it to your User resource prototype. Commented Apr 19, 2013 at 2:13

1 Answer 1

1

You would add ng-class to your li

<ul class="user-list">
    <li ng-repeat="user in users">
        <a ng:click="select(user)">
              {{user.first_name}} {{user.last_name}}
        </a>
    </li>
</ul>

<div class="user-was-selected" ng-show="selectedUser != null">
    <ul class="dropdown-menu">
        <li ng-class="{in-group: selectedUser.inGroup}" ng-repeat="group in groups"><a>{{ group.name }}</a></li>
    </ul>
</div>

You would add the inGroup method to your User resource like this (assuming you have a factory):

angular.module('myApp').factory('User', ['$resource', function($resource) {
  var User = $resource(...);

  ...

  User.prototype.inGroup = function(group) {
    //determine if user is in group
    //'this' (selectedUser) and 'group' are at your disposal
    //return true if there's a match
  };

  ...

  return User;
}]);
Sign up to request clarification or add additional context in comments.

Comments

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.