avoid reloading for group operations

This commit is contained in:
Matteo Pagliazzi
2013-11-01 21:08:36 +01:00
parent 2da33b0355
commit f7dba5f323
4 changed files with 38 additions and 33 deletions

View File

@@ -140,7 +140,9 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
if (confirm("Create Guild for 4 Gems?")) { if (confirm("Create Guild for 4 Gems?")) {
group.$save(function(saved){ group.$save(function(saved){
location.href = '/#/options/groups/guilds/' + saved._id; $scope.groups.guilds.push(saved);
if(saved.privacy === 'public') $scope.groups.public.push(saved);
$state.go('options.social.guilds.detail', {gid: saved._id});
}); });
} }
} }
@@ -153,9 +155,11 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
group = new Groups.Group({_id:group.id}); group = new Groups.Group({_id:group.id});
} }
group.$join(function(){ group.$join(function(joined){
// use https://github.com/angular-ui/ui-router/issues/76 when it's available var i = _.findIndex(User.user.invitations.guilds, {id:joined._id});
location.reload(); if (~i) User.user.invitations.guilds.splice(i,1);
$scope.groups.guilds.push(joined);
$state.go('options.social.guilds.detail', {gid: joined._id});
}) })
} }
@@ -163,9 +167,16 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
if (confirm("Are you sure you want to leave this guild?") !== true) { if (confirm("Are you sure you want to leave this guild?") !== true) {
return; return;
} }
group.$leave(function(){ group.$leave(function(group){
// use https://github.com/angular-ui/ui-router/issues/76 when it's available $scope.groups.guilds.splice(_.indexOf($scope.groups.guilds, group), 1);
location.reload(); // remove user from group members if guild is public so that he can re-join it immediately
if(group.privacy == 'public'){
// slow when a lot of members...? probably yes
group.members = _.without(group.members, function(member){
member._id !== User.user._id;
});
}
$state.go('options.social.guilds');
}); });
} }
@@ -179,18 +190,15 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
} }
]) ])
.controller("PartyCtrl", ['$scope', 'Groups', 'User', .controller("PartyCtrl", ['$scope', 'Groups', 'User', '$state',
function($scope, Groups, User) { function($scope, Groups, User, $state) {
$scope.type = 'party'; $scope.type = 'party';
$scope.text = 'Party'; $scope.text = 'Party';
$scope.group = Groups.groups.party; $scope.group = Groups.groups.party;
$scope.newGroup = new Groups.Group({type:'party', leader: User.user._id, members: [User.user._id]}); $scope.newGroup = new Groups.Group({type:'party', leader: User.user._id, members: [User.user._id]});
$scope.create = function(group){ $scope.create = function(group){
group.$save(function(newGroup){ group.$save(function(newGroup){
// Can't get this to work, group is correctly returned, scope updated but not view.... $scope.group = newGroup;
//Groups.groups.party = newGroup;
//$scope.group = Groups.groups.party;
location.reload();
}); });
} }
@@ -198,10 +206,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
var group = new Groups.Group({_id: party.id, name: party.name}); var group = new Groups.Group({_id: party.id, name: party.name});
// there a better way to access GroupsCtrl.groups.party? // there a better way to access GroupsCtrl.groups.party?
group.$join(function(groupJoined){ group.$join(function(groupJoined){
// Can't get this to work, group is correctly returned, scope updated but not view.... $scope.group = groupJoined;
//Groups.groups.party = groupJoined;
//$scope.group = Groups.groups.party;
location.reload();
}); });
} }
@@ -210,7 +215,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
return; return;
} }
group.$leave(function(){ group.$leave(function(){
Groups.groups.party = undefined; $scope.group = undefined;
}); });
} }

View File

@@ -21,22 +21,16 @@ angular.module('groupServices', ['ngResource']).
// The user may not visit the public guilds, personal guilds, and tavern pages. So // The user may not visit the public guilds, personal guilds, and tavern pages. So
// we defer loading them to the html until they've clicked the tabs // we defer loading them to the html until they've clicked the tabs
var partyQ = $q.defer(), var partyQ = $q.defer();
guildsQ = $q.defer(),
publicQ = $q.defer(),
tavernQ = $q.defer();
var groups = { var groups = {
party: partyQ.promise, party: partyQ.promise
guilds: guildsQ.promise,
public: publicQ.promise,
tavern: tavernQ.promise
}; };
// But we don't defer triggering Party, since we always need it for the header if nothing else // But we don't defer triggering Party, since we always need it for the header if nothing else
Group.get({gid:'party'}, function(party){ Group.get({gid:'party'}, function(party){
partyQ.resolve(party); partyQ.resolve(party);
}) });
return { return {
@@ -44,18 +38,18 @@ angular.module('groupServices', ['ngResource']).
fetchGuilds: _.once(function(){ fetchGuilds: _.once(function(){
//TODO combine these as {type:'guilds,public'} and create a $filter() to separate them //TODO combine these as {type:'guilds,public'} and create a $filter() to separate them
Group.query({type:'guilds'}, function(_groups){ Group.query({type:'guilds'}, function(_groups){
guildsQ.resolve(_groups); groups.guilds = _groups;
//Members.populate(_groups); //Members.populate(_groups);
}); });
Group.query({type:'public'}, function(_groups){ Group.query({type:'public'}, function(_groups){
publicQ.resolve(_groups); groups.public = _groups;
//Members.populate(_groups); //Members.populate(_groups);
}); });
}), }),
fetchTavern: _.once(function(){ fetchTavern: _.once(function(){
Group.get({gid:'habitrpg'}, function(_group){ Group.get({gid:'habitrpg'}, function(_group){
tavernQ.resolve(_group); groups.tavern = _group;
}) })
}), }),

View File

@@ -144,13 +144,19 @@ api.create = function(req, res, next) {
if(err) return res.json(500,{err:err}); if(err) return res.json(500,{err:err});
group.save(function(err, saved){ group.save(function(err, saved){
if (err) return res.json(500,{err:err}); if (err) return res.json(500,{err:err});
return res.json(saved); saved.populate('members', partyFields, function(err, populated){
if (err) return res.json(500,{err:err});
return res.json(populated);
});
}); });
}); });
}else{ }else{
group.save(function(err, saved){ group.save(function(err, saved){
if (err) return res.json(500,{err:err}); if (err) return res.json(500,{err:err});
return res.json(saved); saved.populate('members', partyFields, function(err, populated){
if (err) return res.json(500,{err:err});
return res.json(populated);
});
}); });
} }
} }

View File

@@ -17,7 +17,7 @@ script(type='text/ng-template', id='partials/options.social.party.html')
a.btn.btn-success(data-type='party', ng-click='join(user.invitations.party)') Accept a.btn.btn-success(data-type='party', ng-click='join(user.invitations.party)') Accept
a.btn.btn-danger(ng-click='reject()') Reject a.btn.btn-danger(ng-click='reject()') Reject
// {/} // {/}
div(ng-hide='user.invitations.party', ng-controller='PartyCtrl') div(ng-hide='user.invitations.party')
h2 Create A Party h2 Create A Party
p p
| You are not in a party. You can either create one and invite friends, or if you want to join an existing party, have them enter: | You are not in a party. You can either create one and invite friends, or if you want to join an existing party, have them enter: