PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/js/src/collections/posts.js

https://gitlab.com/craftbynick/Circular
JavaScript | 28 lines | 24 code | 1 blank | 3 comment | 0 complexity | 6695a8035de17e3d78c5423c39524115 MD5 | raw file
  1. Circular.Collections.Posts = Backbone.Collection.extend({
  2. url: "api/posts",
  3. model: Circular.Models.Post,
  4. initialize: function(){
  5. Circular.events.on('ui:posts:sort', this.refreshPostsOrder, this);
  6. },
  7. refreshPostsOrder: function(order){
  8. // Refresh sorting order after jQuery UI sorting:
  9. // (Too bad Underscore doesn't have a function to order an array of objects by an array of one of their attributes)
  10. // @see https://github.com/documentcloud/underscore/issues/692
  11. this.models = this.sortBy(function(post){
  12. return _.indexOf(order, post.id);
  13. });
  14. Circular.events.trigger('posts:sort', order);
  15. },
  16. groupByUser: function(){
  17. var users = {};
  18. _.each(Circular.users, function(value, key){
  19. users[key] = [];
  20. });
  21. var out = this.groupBy(function(post){
  22. return post.get('user');
  23. });
  24. out = _.extend(users, out);
  25. return out;
  26. }
  27. });