responsiv / uploader-plugin

Collection of File Upload components for October
MIT License
62 stars 39 forks source link

Question: When Using imageUploader inside of custom component, scripts are not appended to page #47

Closed oscarnevarezleal closed 6 years ago

oscarnevarezleal commented 6 years ago

Uploaders Scripts are not added unless I manually call OnRun from a component reference. Is this the right thing to do or I´m missing something.

<?php namespace Namespace\Core\Components;

use Cms\Classes\ComponentBase;
use NetSTI\Uploader\Components\ImageUploader;
use RainLab\User\Facades\Auth;
use RainLab\User\Models\User;

/**
 * @property  user
 * @property  imageUploader
 * this component is aliased as 'profile_photos'
 */
class ProfilePhotoUploader extends ComponentBase
{
    private $imageUploader;

    public function componentDetails()
    {
        return [
            'name' => 'ProfilePhotoUploader Component',
            'description' => 'No description provided yet...'
        ];
    }

    /**
     * Returns the logged in user, if available
     */
    public function user()
    {
        if (!Auth::check()) {
            return null;
        }

        return Auth::getUser();
    }

    /**
     *
     */
    public function onRun()
    {
        $this->prepareVars();
        $this->imageUploader->onRun(); // without this, scripts are not added
    }

    /**
     * Executed when this component is initialized
     */
    public function prepareVars()
    {
        $user = $this->user();

        if ($user) {

            /** @var ImageUploader $imageUploader */
            $this->imageUploader = $this->addComponent(
                'NetSTI\Uploader\Components\ImageUploader',
                'imageUploader',
                ['modelClass' => 'RainLab\User\Models\User', 'modelKeyColumn' => 'avatar', 'deferredBinding' => true]
            );

            $this->imageUploader->bindModel('avatar', $user);
            $this->page['user_profile'] = $user;
        }
    }

    public function defineProperties()
    {
        return [

        ];
    }
}
title = "Account"
url = "/account"
layout = "default"

[session]
security = "user"
redirect = "home"

[profile_photos]
==

{% component 'profile_photos' %}

Current user is {{ user.name }}
rajakhoury commented 6 years ago

Did you try to add it in the init() method of your component ?

try

public $user;

public function init()
{
    $this->user = $this->user();
    $this->attachUploader();
}

public function onRun()
{
     $this->page['user_profile'] = $this->user; 
}

public function addUploader()
{
      if ($this->user) {

        $this->imageUploader = $this->addComponent(
            'NetSTI\Uploader\Components\ImageUploader',
            'imageUploader',
            ['modelClass' => 'RainLab\User\Models\User', 'modelKeyColumn' => 'avatar', 'deferredBinding' => true]
        );

        $this->imageUploader->bindModel('avatar',  $this->user ); 
    }
}
oscarnevarezleal commented 6 years ago

Hi @rajakhoury that make the trick. Thanks 👍