challenges: GET /groups now returns a merged array of groups, rather

than structured object of {public,guilds,party,tavern}. I was having
trouble with ngResource without doing this, @paglias let me know what
you think
This commit is contained in:
Tyler Renelle
2013-10-30 15:14:08 -07:00
parent 28cdeb0ac6
commit d2c50dc02f
4 changed files with 24 additions and 21 deletions

View File

@@ -68,3 +68,6 @@ db.groups.find().forEach(function(group){
$pull:{challenges:1}
})
});
// HabitRPG => Tavern
db.groups.update({_id:'habitrpg'}, {$set:{name:'Tavern'}});

View File

@@ -1,14 +1,13 @@
"use strict";
habitrpg.controller("ChallengesCtrl", ['$scope', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state',
function($scope, User, Challenges, Notification, $compile, Groups, $state) {
habitrpg.controller("ChallengesCtrl", ['$scope', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state', 'API_URL', '$http',
function($scope, User, Challenges, Notification, $compile, Groups, $state, API_URL, $http) {
// FIXME get this from cache
Groups.Group.query(function(groups){
groups.tavern[0].name = 'Tavern';
$scope.groups = groups.party.concat(groups.guilds).concat(groups.tavern);
Groups.Group.query({type:'party,guilds,tavern'}, function(groups){
$scope.groups = groups;
$scope.search = {
group: _.reduce($scope.groups, function(m,g){m[g._id]=true;return m;}, {})
group: _.reduce(groups, function(m,g){m[g._id]=true;return m;}, {})
};
});
// FIXME $scope.challenges needs to be resolved first (see app.js)

View File

@@ -42,19 +42,20 @@ angular.module('groupServices', ['ngResource']).
// Note the _.once() to make sure it can never be called again
fetchGuilds: _.once(function(){
//TODO combine these as {type:'guilds,public'} and create a $filter() to separate them
Group.query({type:'guilds'}, function(_groups){
guildsQ.resolve(_groups);
Members.populate(_groups);
//Members.populate(_groups);
});
Group.query({type:'public'}, function(_groups){
publicQ.resolve(_groups);
Members.populate(_groups);
//Members.populate(_groups);
});
}),
fetchTavern: _.once(function(){
Group.get({gid:'habitrpg'}, function(tavern){
tavernQ.resolve(tavern);
Group.get({gid:'habitrpg'}, function(_groups){
tavernQ.resolve(_groups[0]);
})
}),

View File

@@ -41,7 +41,7 @@ api.list = function(req, res) {
var user = res.locals.user;
var groupFields = 'name description memberCount';
var sort = '-memberCount';
var type = (req.query.type || 'party,guilds,public,tavern').split(',');
var type = req.query.type || 'party,guilds,public,tavern';
async.parallel({
@@ -88,15 +88,15 @@ api.list = function(req, res) {
}, function(err, results){
if (err) return res.json(500, {err: err});
// If they're requesting a specific type, let's return it as an array so that $ngResource
// can utilize it properly
if (req.query.type) {
results = _.reduce(type, function(m,t){
return m.concat(_.isArray(results[t]) ? results[t] : [results[t]]);
}, []);
}
res.json(results);
// ngResource expects everything as arrays. We used to send it down as a structured object: {public:[], party:{}, guilds:[], tavern:{}}
// but unfortunately ngResource top-level attrs are considered the ngModels in the list, so we had to do weird stuff and multiple
// requests to get it to work properly. Instead, we're not depending on the client to do filtering / organization, and we're
// just sending down a merged array. Revisit
var arr = _.reduce(results, function(m,v){
if (_.isEmpty(v)) return m;
return m.concat(_.isArray(v) ? v : [v]);
}, [])
res.json(arr);
})
};