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