Ejemplo n.º 1
1
 /**
  * @inheritdoc
  * @throw NotSupportedException
  */
 public function run()
 {
     if ($this->hasModel()) {
         if (!is_null($this->value)) {
             if (!in_array($this->attribute, $this->model->attributes())) {
                 throw new NotSupportedException('Unable to set value of the property \'' . $this->attribute . '\'.');
             }
             $stash = $this->model->{$this->attribute};
             $this->model->{$this->attribute} = $this->value;
         }
         $output = Html::activeListBox($this->model, $this->attribute, $this->items, $this->options);
         if (isset($stash)) {
             $this->model->{$this->attribute} = $stash;
         }
     } else {
         $output = Html::listBox($this->name, $this->value, $this->items, $this->options);
     }
     $js = 'jQuery(\'#' . $this->options['id'] . '\').select2(' . Json::htmlEncode($this->clientOptions) . ');';
     if (Yii::$app->getRequest()->getIsAjax()) {
         $output .= Html::script($js);
     } else {
         $view = $this->getView();
         Select2Asset::register($view);
         Select2LanguageAsset::register($view);
         $view->registerJs($js);
     }
     return $output;
 }
 public function testBundle()
 {
     $bundle = Yii::$app->getAssetManager()->getBundle(Select2Asset::className());
     $this->assertInstanceOf('yii\\jquery\\select2\\Select2Asset', $bundle);
     $this->assertArrayHasKey(0, $bundle->depends);
     $this->assertEquals('yii\\web\\JqueryAsset', $bundle->depends[0]);
     $this->assertArrayHasKey(0, $bundle->js);
     $this->assertFileExists($bundle->basePath . DIRECTORY_SEPARATOR . $bundle->js[0]);
     $this->assertArrayHasKey(0, $bundle->css);
     $this->assertFileExists($bundle->basePath . DIRECTORY_SEPARATOR . $bundle->css[0]);
 }