metamaps--metamaps/app/assets/javascripts/metamaps/Metamaps.Backbone.js

131 lines
4.4 KiB
JavaScript
Raw Normal View History

Metamaps.Backbone = {};
Metamaps.Backbone.Map = Backbone.Model.extend({
urlRoot: '/maps',
2014-08-12 15:09:53 +00:00
blacklist: ['created_at', 'updated_at', 'user_name', 'topic_count', 'synapse_count', 'topics', 'synapses', 'mappings', 'mappers'],
toJSON: function (options) {
return _.omit(this.attributes, this.blacklist);
},
authorizeToEdit: function (mapper) {
if (mapper && (this.get('permission') === "commons" || this.get('user_id') === mapper.get('id'))) return true;
else return false;
2014-08-10 17:06:58 +00:00
},
getUser: function () {
return Metamaps.Mapper.get(this.get('user_id'));
},
2014-08-11 22:57:34 +00:00
fetchContained: function () {
var bb = Metamaps.Backbone;
2014-08-12 02:31:28 +00:00
var that = this;
2014-08-11 22:57:34 +00:00
var start = function (data) {
2014-08-12 02:31:28 +00:00
that.set('mappers', new bb.MapperCollection(data.mappers));
that.set('topics', new bb.TopicCollection(data.topics));
that.set('synapses', new bb.SynapseCollection(data.synapses));
that.set('mappings', new bb.MappingCollection(data.mappings));
2014-08-11 22:57:34 +00:00
}
$.ajax({
2014-08-12 02:31:28 +00:00
url: "/maps/" + this.id + "/contains.json",
2014-08-11 22:57:34 +00:00
success: start,
async: false
});
},
2014-08-10 17:06:58 +00:00
getTopics: function () {
if (!this.get('topics')) {
2014-08-11 22:57:34 +00:00
this.fetchContained();
2014-08-10 17:06:58 +00:00
}
return this.get('topics');
},
getSynapses: function () {
if (!this.get('synapses')) {
2014-08-11 22:57:34 +00:00
this.fetchContained();
2014-08-10 17:06:58 +00:00
}
return this.get('synapses');
},
2014-08-11 22:57:34 +00:00
getMappings: function () {
if (!this.get('mappings')) {
this.fetchContained();
}
return this.get('mappings');
},
getMappers: function () {
if (!this.get('mappers')) {
this.fetchContained();
}
return this.get('mappers');
},
2014-08-10 17:06:58 +00:00
attrForCards: function () {
var obj = {
id: this.id,
name: this.get('name'),
desc: this.get('desc'),
2014-08-12 15:09:53 +00:00
username: this.get('user_name'),
mkPermission: this.get("permission") ? this.get("permission").substring(0, 2) : "co",
2014-08-10 17:06:58 +00:00
editPermission: this.authorizeToEdit(Metamaps.Active.Mapper) ? 'canEdit' : 'cannotEdit',
2014-08-12 15:09:53 +00:00
topicCount: this.get('topic_count'),
synapseCount: this.get('synapse_count'),
2014-08-10 17:06:58 +00:00
createdAt: this.get('created_at')
};
return obj;
}
});
Metamaps.Backbone.MapsCollection = Backbone.Collection.extend({
model: Metamaps.Backbone.Map,
2014-08-10 17:06:58 +00:00
initialize: function(models, options) {
this.id = options.id;
this.sortBy = options.sortBy;
},
url: function() {
return '/explore/' + this.id + '.json';
},
comparator: function (a, b) {
a = a.get(this.sortBy);
b = b.get(this.sortBy);
2014-08-12 02:31:28 +00:00
var temp;
2014-08-10 17:06:58 +00:00
if (this.sortBy === 'name') {
2014-08-10 20:20:21 +00:00
a = a ? a.toLowerCase() : "";
b = b ? b.toLowerCase() : "";
2014-08-10 17:06:58 +00:00
}
2014-08-12 02:31:28 +00:00
else {
// this is for updated_at and created_at
temp = a;
a = b;
b = temp;
}
2014-08-10 17:06:58 +00:00
return a > b ? 1 : a < b ? -1 : 0;
},
getMaps: function () {
var self = this;
this.fetch({
reset: true,
success: function (collection, response, options) {
// you can pass additional options to the event you trigger here as well
self.trigger('successOnFetch');
},
error: function (collection, response, options) {
// you can pass additional options to the event you trigger here as well
self.trigger('errorOnFetch');
}
});
}
});
Metamaps.Backbone.Mapper = Backbone.Model.extend({
urlRoot: '/users',
blacklist: ['created_at', 'updated_at'],
toJSON: function (options) {
return _.omit(this.attributes, this.blacklist);
},
prepareLiForFilter: function () {
var li = '';
li += '<li data-id="' + this.id.toString() + '">';
li += '<img src="/assets/icons/person.png" data-id="' + this.id.toString() + '"';
li += ' alt="' + this.get('name') + '" />';
li += '<p>' + this.get('name') + '</p></li>';
return li;
}
});
Metamaps.Backbone.MapperCollection = Backbone.Collection.extend({
model: Metamaps.Backbone.Mapper,
url: '/users'
2014-07-31 01:10:10 +00:00
});