cartodb-4.42/lib/assets/javascripts/builder/components/custom-carousel/custom-carousel-collection.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-04-06 13:25:13 +08:00
var Backbone = require('backbone');
var CustomCarouselModel = require('./custom-carousel-item-model');
/*
* Custom list collection, it parses pairs like:
*
* [{ val, label }]
* ["string"]
*/
module.exports = Backbone.Collection.extend({
model: function (attrs, opts) {
var d = {};
if (typeof attrs === 'string') {
d.val = attrs;
} else {
d = attrs;
}
return new CustomCarouselModel(d);
},
initialize: function () {
this._initBinds();
},
_initBinds: function () {
this.bind('change:selected', this._onSelectedChange, this);
},
_onSelectedChange: function (changedModel, isSelected) {
if (isSelected) {
this.each(function (m) {
if (m.cid !== changedModel.cid && m.get('selected')) {
m.set('selected', false);
}
});
}
},
getSelected: function () {
return this.findWhere({ selected: true });
},
getSelectedValue: function () {
var selectedModel = this.getSelected();
return selectedModel && selectedModel.get('val');
},
getHighlighted: function () {
return this.findWhere({ highlighted: true });
}
});