Merge branch 'hotfix/21.56.9' into master
[gitter.git] / public / js / collections / tag-collection.js
blobb6f41eae62e844219f8e3e960f4e30487f69932f
1 'use strict';
3 var Backbone = require('backbone');
4 var context = require('gitter-web-client-context');
5 var validateTag = require('gitter-web-shared/validation/validate-tag').validateTag;
7 var TagModel = Backbone.Model.extend({
8 defaults: {
9 value: ''
12 //we get an array of tag strings from the server
13 //rather than { value: '' }
14 //we need to parse them here
15 initialize: function(tag) {
16 this.set('value', tag);
19 validate: function(attrs) {
20 var isStaff = context.isStaff();
21 var result = validateTag(attrs.value, isStaff);
23 if (result.messages.length > 0) {
24 return result.messages.join(' ');
27 });
29 var TagCollection = Backbone.Collection.extend({
30 model: TagModel,
32 addModel: function(model) {
33 var val = model.get('value');
35 //if there is a duplicate fire error
36 if (this.where({ value: val }).length) {
37 this.trigger('tag:error:duplicate', val);
38 } else {
39 this.add(model);
40 this.trigger('tag:added', val);
44 toJSON: function() {
45 return this.reduce(function(memo, model) {
46 memo.push(model.get('value'));
47 return memo;
48 }, []).join(',');
50 });
52 module.exports = {
53 TagCollection: TagCollection,
54 TagModel: TagModel