3 var Backbone
= require('backbone');
4 var LiveCollection
= require('gitter-realtime-client').LiveCollection
;
5 var realtime
= require('../components/realtime');
6 var SyncMixin
= require('./sync-mixin');
7 var context
= require('gitter-web-client-context');
9 var GroupModel
= Backbone
.Model
.extend({
15 defaultRoomName
: 'community',
24 var GroupCollection
= LiveCollection
.extend({
26 urlTemplate
: '/v1/user/:userId/groups',
27 contextModel
: context
.contextModel(),
29 return realtime
.getClient();
33 initialize: function() {
34 this.listenTo(this, 'change:favourite', this.reorderFavs
);
37 reorderFavs: function(model
) {
39 * We need to do some special reordering in the model of a favourite being positioned
40 * This is to mirror the changes happening on the server
41 * @see recent-room-service.js@addTroupeAsFavouriteInPosition
44 /* This only applies when a fav has been set */
45 if (!model
.changed
|| !model
.changed
.favourite
|| this.reordering
) {
49 this.reordering
= true;
51 var favourite
= model
.changed
.favourite
;
53 var forUpdate
= this.map(function(room
) {
54 return { id
: room
.id
, favourite
: room
.get('favourite') };
55 }).filter(function(room
) {
56 return room
.favourite
>= favourite
&& room
.id
!== model
.id
;
59 forUpdate
.sort(function(a
, b
) {
60 return a
.favourite
- b
.favourite
;
64 for (var i
= 0; i
< forUpdate
.length
; i
++) {
65 var item
= forUpdate
[i
];
67 if (item
.favourite
> next
) {
68 forUpdate
.splice(i
, forUpdate
.length
);
73 next
= item
.favourite
;
77 for (var j
= forUpdate
.length
- 1; j
>= 0; j
--) {
80 var value
= r
.favourite
;
82 t
.set('favourite', value
, { silent
: true });
85 delete this.reordering
;
91 Collection
: GroupCollection