Reputation: 2424
I'm trying to implement an asynchronous follow/unfollow function.. just like in instagram when you pull up the list of your followers there's an 'following'/'follow' button associated to each item.. for brevity i made the code simple. I'm using ionic framework
my code in the view:
<ion-list>
<ion-item ng-repeat="user in users">
<span ng-click="followUser(user.id, indexHowTo??)">Follow</span>
<p>{{user.name}}</p>
</ion-item>
</ion-list>
$scope.followUser = function(userid, indexHowTo) {
var to_from = {
to_user: userid,
from_user: $localStorage.CurrentUser.id
}
UserService.FollowUser(to_from, $localStorage.CurrentUser.auth_token)
.success(function(data) {
$scope.users[index].is_following = true; //i'll do something in the view just didn't show for brevity
}).
error(function(error, status) {
//alert(status);
console.log(error);
});
}
Upvotes: 2
Views: 216
Reputation: 193261
You don't really need any index at all, just pass user object in the function:
<ion-list>
<ion-item ng-repeat="user in users">
<span ng-click="followUser(user)">Follow</span>
<p>{{user.name}}</p>
</ion-item>
</ion-list>
And use it this way:
$scope.followUser = function (user) {
var to_from = {
to_user: user.id,
from_user: $localStorage.CurrentUser.id
};
UserService.FollowUser(to_from, $localStorage.CurrentUser.auth_token)
.success(function (data) {
user.is_following = true;
}).
error(function (error, status) {
//alert(status);
console.log(error);
});
}
So your code becomes cleaner and simpler.
Upvotes: 3