From d2a24fd7833479e97233b391f6c5ec71ae210c95 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Wed, 20 Jun 2018 00:55:14 +0200 Subject: [PATCH] Add `setOptions`, `addOption` and `getOptions` to PropertyRadio. Closes #1215 --- src/style_manager/model/PropertyRadio.js | 29 +++++++++++++++++++- src/style_manager/view/PropertySelectView.js | 16 +++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/style_manager/model/PropertyRadio.js b/src/style_manager/model/PropertyRadio.js index 959711154..1ca862471 100644 --- a/src/style_manager/model/PropertyRadio.js +++ b/src/style_manager/model/PropertyRadio.js @@ -1,9 +1,36 @@ const Property = require('./Property'); module.exports = Property.extend({ - defaults: { + defaults: () => ({ ...Property.prototype.defaults, // Array of options, eg. [{name: 'Label ', value: '100'}] options: [] + }), + + initialize(...args) { + Property.prototype.initialize.apply(this, args); + this.listenTo(this, 'change:options', this.onOptionChange); + }, + + onOptionChange() { + this.set('list', this.get('options')); + }, + + getOptions() { + const { options, list } = this.attributes; + return options && options.length ? options : list; + }, + + setOptions(opts = []) { + this.set('options', opts); + return this; + }, + + addOption(opt) { + if (opt) { + const opts = this.getOptions(); + this.setOptions([...opts, opt]); + } + return this; } }); diff --git a/src/style_manager/view/PropertySelectView.js b/src/style_manager/view/PropertySelectView.js index 3b3fdde7f..cf54eef85 100644 --- a/src/style_manager/view/PropertySelectView.js +++ b/src/style_manager/view/PropertySelectView.js @@ -1,7 +1,8 @@ import Backbone from 'backbone'; +import PropertyView from './PropertyView'; const $ = Backbone.$; -module.exports = require('./PropertyView').extend({ +module.exports = PropertyView.extend({ templateInput() { const pfx = this.pfx; const ppfx = this.ppfx; @@ -15,10 +16,19 @@ module.exports = require('./PropertyView').extend({ `; }, + initialize(...args) { + PropertyView.prototype.initialize.apply(this, args); + this.listenTo(this.model, 'change:options', this.updateOptions); + }, + + updateOptions() { + this.input = null; + this.onRender(); + }, + onRender() { var pfx = this.pfx; - const model = this.model; - const options = model.get('list') || model.get('options') || []; + const options = this.model.getOptions(); if (!this.input) { let optionsStr = '';