"use strict";

const _ = require( 'underscore' );
const Backbone = require( 'backbone' );
//require( 'backbone-event-logger' );

const VariableListCollection = require( '../collections/dirPickerVariableList' );

//noinspection JSUnusedGlobalSymbols
/**
 * @class
 * @extends {Backbone.Model}
 */
const ModelsDirPickerVariable = Backbone.Model.extend( {
  /**
   * @type {CollectionsDirPickerVariableList}
   */
  listCollection: undefined,
  defaults      : {
    "name": '名称未設変数',
    "list": []
  },
  initialize    : function ( attributes ) {
    //this.debugEvents('ModelsVariable');
    const _self = this;
    this.set( 'name', this.makeUniqueName( (attributes && attributes.name) || this.defaults.name ) );
    this.set( 'list', (attributes && attributes.list) || this.defaults.list );
    this.listCollection = new VariableListCollection( this.get( 'list' ) );
    this.listCollection.on( 'change', function () {
      _self.updateList();
    } );
    this.listCollection.on( 'add', function () {
      _self.trigger( 'badgeUpdate' );
      _self.updateList();
    } );
    this.listCollection.on( 'remove', function () {
      _self.trigger( 'badgeUpdate' );
      _self.updateList();
    } );
  },
  updateList    : function () {
    this.save( 'list', _.map( this.listCollection.sort( {silent: true} ).toJSON(), function ( listRow ) {
      return _.pick( listRow, 'label', 'val' );
    } ), {wait: true, silent: true} );
  }
} );
_.extend( ModelsDirPickerVariable.prototype, require( './mixin' ) );

module.exports = ModelsDirPickerVariable;

@@ -12,126 +12,96 @@
 %22;%0A%0A
-const _ = require( 'underscore' );%0Aconst Backbone = require( 'backbone' );%0A//require( 'backbone-event-logger' );%0A%0Acons
+import _ from 'underscore';%0Aimport DirPickerModelBase from './dirPickerModelBase';%0Aimpor
 t Va
@@ -125,18 +125,12 @@
 ion 
-= require(
+from
  '..
@@ -168,154 +168,104 @@
 ist'
- )
 ;%0A%0A/
-/noinspection JSUnusedGlobalSymbols%0A/**%0A * @class%0A * @extends %7BBackbone.Model%7D%0A */%0Aconst ModelsDirPickerVariable = Backbone.Model.extend(
+**%0A * %E5%AE%9A%E7%BE%A9%E6%B8%88%E3%81%BF%E5%A4%89%E6%95%B0%E3%82%92%E8%A1%A8%E3%81%99%E3%83%A2%E3%83%87%E3%83%AB%0A */%0Aexport default class DirPickerVariable extends DirPickerModelBase
  %7B%0A
+%0A
   /*
@@ -276,195 +276,133 @@
  * @
-type %7BCollectionsDirPickerVariableList%7D%0A   */%0A  listCollection: undefined,%0A  defaults      : %7B%0A    %22name%22: '%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%A4%89%E6%95%B0',%0A    %22list%22: %5B%5D%0A  %7D,%0A  initialize    : function ( attributes ) %7B
+param %7Bobject%7D %5Battr%5D%0A   * @param %7Bobject%7D %5Boptions%5D%0A   */%0A  constructor ( attr, options ) %7B%0A    super( attr, options );
 %0A    
-//
 this
@@ -414,16 +414,17 @@
 gEvents(
+ 
 'ModelsV
@@ -435,34 +435,12 @@
 ble'
+ 
 );%0A
-    const _self = this;
 %0A   
@@ -480,38 +480,32 @@
 eName( (attr
-ibutes
  && attr
 ibutes.name)
@@ -492,22 +492,16 @@
  && attr
-ibutes
 .name) %7C
@@ -557,22 +557,16 @@
 attr
-ibutes
  && attr
 ibut
@@ -565,14 +565,8 @@
 attr
-ibutes
 .lis
@@ -592,16 +592,70 @@
 .list );
+%0A%0A    /**%0A     * @type %7BDirPickerVariableList%7D%0A     */
 %0A    thi
@@ -757,35 +757,28 @@
 ( 'change', 
-function 
 ()
+=%3E
  %7B%0A      _se
@@ -770,29 +770,28 @@
 )=%3E %7B%0A      
-_self
+this
 .updateList(
@@ -834,30 +834,30 @@
 on( 'add
-', function
+ remove',
  ()
+=%3E
  %7B%0A     
@@ -849,37 +849,36 @@
 ', ()=%3E %7B%0A      
-_self
+this
 .trigger( 'badge
@@ -886,37 +886,36 @@
 pdate' );%0A      
-_self
+this
 .updateList();%0A 
@@ -926,164 +926,301 @@
  );%0A
-    this.listCollection.on( 'remove', function () %7B
+%0A    this.on( 'destroy', ()=%3E %7B%0A      this.listCollection.off();%0A    %7D );%0A  %7D%0A%0A  //noinspection JSMethodCanBeStatic%0A  /**
 %0A   
+*%0A
    
-_self.trigger( 'badgeUpdate' );%0A      _self.updateList();
+* @returns %7B%7Bname: '%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%A4%89%E6%95%B0', list: Array%7D%7D%0A   */%0A  get defaults () %7B%0A    return %7B%0A      %22name%22: '%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%A4%89%E6%95%B0',%0A      %22list%22: %5B%5D
 %0A    %7D
- );
 %0A  %7D
-,%0A  updateList    : function
+%0A%0A  /**%0A   *%0A   */%0A  updateList
  () 
@@ -1307,17 +1307,8 @@
 N(),
- function
  ( l
@@ -1315,16 +1315,18 @@
 istRow )
+=%3E
  %7B%0A     
@@ -1416,120 +1416,5 @@
  %7D%0A%7D
- );%0A_.extend( ModelsDirPickerVariable.prototype, require( './mixin' ) );%0A%0Amodule.exports = ModelsDirPickerVariable;
 %0A

Refactor to ES6