cartodb-4.42/lib/assets/javascripts/dashboard/data/like-model.js

69 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-04-06 13:25:13 +08:00
var Backbone = require('backbone');
var _ = require('underscore');
const checkAndBuildOpts = require('builder/helpers/required-opts');
const REQUIRED_OPTS = [
'config'
];
var LikeModel = Backbone.Model.extend({
defaults: {
likeable: true
},
url: function (method) {
var version = this._config.urlVersion('like', method);
return `${this._config.get('base_url')}/api/${version}/viz/${this.get('vis_id')}/like`;
},
initialize: function (attrs, options) {
checkAndBuildOpts(options, REQUIRED_OPTS, this);
_.bindAll(this, '_onSaveError');
this.on('destroy', function () {
this.set({
liked: false,
likes: this.get('likes') - 1
});
}, this);
},
_onSaveError: function (model, response) {
this.trigger('error', {
status: response.status,
statusText: response.statusText
});
},
toggleLiked: function () {
if (this.get('liked')) {
this.destroy();
} else {
this.set({ id: null }, { silent: true });
this.save({}, { error: this._onSaveError });
}
}
}, {
newByVisData: function (opts) {
var d = _.defaults({
id: opts.liked ? opts.vis_id : null
}, _.omit(opts, 'url', 'config'));
var model = new LikeModel(d, {
config: opts.config
});
if (opts.url) {
model.url = opts.url;
}
return model;
}
});
module.exports = LikeModel;