Skip to content

Commit

Permalink
combined request to avoid many requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Oct 23, 2013
1 parent 14ff3a4 commit 7d68bed
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -8,9 +8,15 @@ is usable.
The original code:
[20130804_recorded](https://github.com/dai-shi/twitter-clone-sample/tree/20130804_recorded)
is exactly the same as
[screencast](http://dai-shi.github.io/social-cms-backend/ttyplay.html).
[screencast](http://dai-shi.github.io/social-cms-backend/ttyplay.html)
result.

The running code:
[20130920_openshift](https://github.com/dai-shi/twitter-clone-sample/tree/20130920_openshift)
can be tried at
[OpenShift](http://twitterclonesample-nodeangularapp.rhcloud.com/).
[OpenShift server](http://twitterclonesample-nodeangularapp.rhcloud.com/).

changes
-------

* use combined request to avoid many requests for getting likecount for each post.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -9,6 +9,6 @@
"dependencies": {
"express": "3.3.4",
"jade": "*",
"social-cms-backend": "0.0.7"
"social-cms-backend": "0.2.3"
}
}
54 changes: 42 additions & 12 deletions public/javascripts/main.js
Expand Up @@ -13,32 +13,62 @@ factory('Like', function($resource) {
}).

controller('MyController', function($scope, User, Post, Like) {
$scope.myself = User.get({id: 'myself'});
$scope.myself = User.get({
id: 'myself'
});
$scope.userinfo = {};
$scope.ensureUserinfo = function(user_id) {
if ($scope.userinfo[user_id]) return;
$scope.userinfo[user_id] = User.get({id: user_id});
$scope.userinfo[user_id] = User.get({
id: user_id
});
};

$scope.posts = Post.query();
$scope.likecount = {};

Post.query(function(data) {
$scope.posts = data;

var queries = _.map(data, function(post) {
return {
post_id: post._id
};
});
Like.query({
id: 'count',
query: JSON.stringify(queries)
}, function(results) {
for (var i = 0; i < data.length; i++) {
$scope.likecount[data[i]._id] = results[i];
}
});
});

$scope.postNewPost = function() {
if (!$scope.newMessage) return;
Post.save({message: $scope.newMessage}, function() {
Post.save({
message: $scope.newMessage
}, function() {
$scope.posts = Post.query();
});
$scope.newMessage = null;
};

$scope.likecount = {};
$scope.ensureLikecount = function(post_id, refresh) {
if ($scope.likecount[post_id] && !refresh) return;
$scope.likecount[post_id] = Like.get({id: 'count',
query: JSON.stringify({post_id: post_id})});
};
$scope.addLikecount = function(post_id) {
Like.save({post_id: post_id}, function() {
$scope.ensureLikecount(post_id, true);
Like.save({
post_id: post_id
}, function() {
$scope.likecount[post_id].count = '...';
Like.get({
id: 'count',
query: JSON.stringify({
post_id: post_id
})
},

function(data) {
$scope.likecount[post_id] = data;
});
});
};

Expand Down
2 changes: 1 addition & 1 deletion views/index.jade
Expand Up @@ -12,7 +12,7 @@ block content
hr
p What others are doing:
.well.well-small(ng-repeat='post in posts')
div(ng-init='ensureLikecount(post._id)')
div
span {{post.message}}
span.pull-right
a(href='javascript:void(0)', ng-click='addLikecount(post._id)') Like
Expand Down
1 change: 1 addition & 0 deletions views/layout.jade
Expand Up @@ -8,6 +8,7 @@ html(ng-app='MyModule')
script(src='//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/js/bootstrap.min.js')
script(src='//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js')
script(src='//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.min.js')
script(src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js")
script(src='/javascripts/main.js')
body
block content

0 comments on commit 7d68bed

Please sign in to comment.