Browse Source

Refactor TraitView and sync it with target changes. Closes #686

pull/712/head
Artur Arseniev 9 years ago
parent
commit
4ef22bddf0
  1. 45
      src/trait_manager/model/Trait.js
  2. 44
      src/trait_manager/view/TraitView.js

45
src/trait_manager/model/Trait.js

@ -1,6 +1,6 @@
var Backbone = require('backbone'); import { isUndefined } from 'underscore';
module.exports = Backbone.Model.extend({ module.exports = require('backbone').Model.extend({
defaults: { defaults: {
type: 'text', // text, number, range, select type: 'text', // text, number, range, select
@ -16,13 +16,50 @@ module.exports = Backbone.Model.extend({
options: [], options: [],
}, },
initialize() { initialize() {
if (this.get('target')) { const target = this.get('target');
this.target = this.get('target'); const name = this.get('name');
const changeProp = this.get('changeProp');
if (target) {
this.target = target;
this.unset('target'); this.unset('target');
const targetEvent = changeProp ? `change:${name}` : `change:attributes:${name}`;
this.listenTo(target, targetEvent, this.targetUpdated);
}
},
targetUpdated() {
const value = this.getTargetValue();
!isUndefined(value) && this.set({ value }, { fromTarget: 1 });
},
getTargetValue() {
const name = this.get('name');
const target = this.target;
const prop = this.get('changeProp');
if (target) return prop ? target.get(name) : target.getAttributes()[name];
},
setTargetValue(value) {
const target = this.target;
const name = this.get('name');
if (isUndefined(value)) return;
if (this.get('changeProp')) {
target.set(name, value);
} else {
const attrs = { ...target.get('attributes') };
attrs[name] = value;
target.set('attributes', attrs);
} }
}, },
/** /**
* Get the initial value of the trait * Get the initial value of the trait
* @return {string} * @return {string}

44
src/trait_manager/view/TraitView.js

@ -1,3 +1,6 @@
import { isUndefined, clone } from 'underscore';
const Backbone = require('backbone');
const $ = Backbone.$; const $ = Backbone.$;
module.exports = Backbone.View.extend({ module.exports = Backbone.View.extend({
@ -11,20 +14,23 @@ module.exports = Backbone.View.extend({
}, },
initialize(o) { initialize(o) {
var md = this.model; const model = this.model;
const name = model.get('name');
const target = model.target;
this.config = o.config || {}; this.config = o.config || {};
this.pfx = this.config.stylePrefix || ''; this.pfx = this.config.stylePrefix || '';
this.ppfx = this.config.pStylePrefix || ''; this.ppfx = this.config.pStylePrefix || '';
this.target = md.target; this.target = target;
this.className = this.pfx + 'trait'; this.className = this.pfx + 'trait';
this.labelClass = this.ppfx + 'label'; this.labelClass = this.ppfx + 'label';
this.fieldClass = this.ppfx + 'field ' + this.ppfx + 'field-' + md.get('type'); this.fieldClass = this.ppfx + 'field ' + this.ppfx + 'field-' + model.get('type');
this.inputhClass = this.ppfx + 'input-holder'; this.inputhClass = this.ppfx + 'input-holder';
md.off('change:value', this.onValueChange); model.off('change:value', this.onValueChange);
this.listenTo(md, 'change:value', this.onValueChange); this.listenTo(model, 'change:value', this.onValueChange);
this.tmpl = '<div class="' + this.fieldClass +'"><div class="' + this.inputhClass +'"></div></div>'; this.tmpl = '<div class="' + this.fieldClass +'"><div class="' + this.inputhClass +'"></div></div>';
}, },
/** /**
* Fires when the input is changed * Fires when the input is changed
* @private * @private
@ -37,22 +43,26 @@ module.exports = Backbone.View.extend({
return this.model.get('value'); return this.model.get('value');
}, },
setInputValue(value) {
this.getInputEl().value = value;
},
/** /**
* On change callback * On change callback
* @private * @private
*/ */
onValueChange() { onValueChange(model, value, opts = {}) {
var m = this.model; const mod = this.model;
var trg = this.target; const trg = this.target;
var name = m.get('name'); const name = mod.get('name');
var value = this.getValueForTarget();
// Chabge property if requested otherwise attributes if (opts.fromTarget) {
if(m.get('changeProp')){ this.setInputValue(mod.get('value'));
trg.set(name, value); } else {
}else{ const value = this.getValueForTarget();
var attrs = _.clone(trg.get('attributes')); mod.setTargetValue(value);
attrs[name] = value;
trg.set('attributes', attrs);
} }
}, },

Loading…
Cancel
Save