-
-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to using in livewire #789
Comments
@Saeeed-B Sorry, I don't know well of the livewire framework. :( |
Are u still interested in how to use SunEditor with Livewire & Alpine.JS? I've implemented this just a few days ago and it work's like a charme until now 8-) Could write a possible solution on interest... |
Absolutely! I think it would be very good. |
Okay, here we go 8-) In our Livewire component, we need a public variable to store the content. This could be a 'normal' string variable or an eloquent field from our model (e.g. public Post $post;). Make sure you include this field in the validation rules() of the livewire component to get the form attributes after submitting it (e.g. $attributes = $this->getValidatedAttributes()). Take a look at the livewire documentation if you're not familiar with this form processing stuff. To be able to use the SunEditor in our form, the easiest way is to create a blade component for this. This component can then be used in our Livewire blade template as usual:
Use the "defer" directive to get rid of unneccessary server calls. Now we take a look at our blade component. To be able to use multiple SunEditor's in one form, we first need to create unique names for all instances. The simpliest way is to include a small php call in our blade component for this:
The $name variable includes now the name of the linked wire model (i.e. "post.description") and can be used for the textarea and an optional html label tag:
The "$slot" will be replaced with our content above ("My post content") and is used for the textarea label. The textarea itself injects Alpine.js via the "x-data" directive and uses the "@entangle" keyword to 2-way-link Alpine with our Livewire model. The "x-cloak" custom html attribute is used to hide the textarea while loading the content. It's defined in the "app.css" file of our Laravel application:
I always try to separate larger Alpine.js logic from my blade components, so I call the "editor" function which code is placed in it's own file. So let's take a look at the "app.js" file of our app first:
We include the SunEditor functionality by 'require'-ing the separate Javascript file (suneditor.js) in the 'components'-subfolder:
First, we need to make sure that Alpine.js has finished it's initialization stuff. Then we define our 'editor'-function with accepts just two parameters: "el" and "model". The "el" passes the textarea element from the dom to be used and the "model" holds the Livewire model defined. Then we create a new SunEditor instance with the plugins to use (and some other stuff you need). To fill the editor with the previously stored content, we only need to set the 'value' variable. To update the wire model, we hook into the "onChange" callback from SunEditor. At this point, we just fill the value with the new content - that's it. |
i use like this:
this is very simple and apline is not required |
@JiHong88 hi.
How to using with livewire framework?
How to use wire:model?
With Ex please.
The text was updated successfully, but these errors were encountered: