Opened 10 years ago
Closed 10 years ago
#12952 closed New Feature (fixed)
[FileLoader] Customization of the communication with the server should be simpler
Reported by: | Artur Delura | Owned by: | Piotr Jasiun |
---|---|---|---|
Priority: | Normal | Milestone: | CKEditor 4.5.0 Beta |
Component: | General | Version: | |
Keywords: | Cc: |
Description (last modified by )
Working on ticket:12612 we found out that we need to allow user to add flag withCredentials
and some extra headers to XHR object in filetools plugins. Use case for this is to allow cross domain requests.
Following this example I tried to customize a bit my request object. I end up with:
CKEDITOR.replace( 'editor', function() { CKEDITOR.fileTools.fileLoader.prototype.sendRequest = function() { var formData = new FormData(), xhr = this.xhr; formData.append( 'upload', this.file, this.fileName ); xhr.withCredentials = true; // Extra lines xhr.setRequestHeader( 'X-PINGOTHER', 'pingpong' ); // Extra lines xhr.open( 'POST', this.uploadUrl, true ); xhr.send( formData ); }; } );
Shortcomings of this solution are:
- We overwrite global function
sendRequest
which influence all editor instances while optionimageUploadUrl
is unique for all editor isntances. - We have to wait for plugin to be loaded and editor instance as well to overwrite this method. If user have got a few editors he have to impelement special mechanism to overwrite this function when first editor instance is loaded. Or maybe there is a global event which fire when plugin is loaded?
We got at least two sulutions for this:
- We can add extra config option like:
xhrData = { withCredentials: true, headers: { 'X-HEADER-NAME': "X-HEADER VALUE" } };
And function send request will take into consideration xhrData
config options and set flag and headers. This is simples solution but don't allow user whole freedom.
- Second option is to fire events like
prerequest
which will have an xhr object as a property and user would make whatever he wants with this.
Change History (12)
comment:1 Changed 10 years ago by
Description: | modified (diff) |
---|
comment:2 Changed 10 years ago by
comment:3 Changed 10 years ago by
Milestone: | → CKEditor 4.5.0 |
---|---|
Summary: | Modifying XHR object in filetools plugin is painfull. → [FileLoader] Customization of the communication with the server should be simpler |
comment:4 Changed 10 years ago by
Type: | Bug → New Feature |
---|
comment:5 Changed 10 years ago by
Status: | new → confirmed |
---|
comment:6 Changed 10 years ago by
Owner: | set to Artur Delura |
---|---|
Status: | confirmed → assigned |
comment:7 Changed 10 years ago by
Status: | assigned → review |
---|
I removed both sendRequest
and handleResponse
private method and added fileUploadRequest
and fileUploadResponse
events with appropriate documentation and examples. Tests has been updated as well. Changes in branch:t/12952.
comment:8 Changed 10 years ago by
Owner: | changed from Artur Delura to Piotr Jasiun |
---|---|
Status: | review → assigned |
This ticket needs some API improvements.
comment:9 Changed 10 years ago by
Status: | assigned → review |
---|
Changes done and waiting for the review. Changes in t/12952.
comment:10 Changed 10 years ago by
Status: | review → review_failed |
---|
I pushed rebased branch:t/12952 plus one additional commit. We decided to move events to the editor instance, though, so R- for now.
comment:12 Changed 10 years ago by
Resolution: | → fixed |
---|---|
Status: | review → closed |
Merged to major with git:a5d0bf2.
First of all we should talk general, about the pair of the methods:
sendRequest
andhandleResponse
. Most probably they will be redefined together to adapt the request to the server needs and handle the response. Note the by default data are send as a form with hardcoded field name and response needs to be a JSON with defined structure, so user may want to change it.At the first approach we decided that we will not use events, but user will overwrite these methods, because:
Now we know that:
withCredentials
) and another part want to define format of the data;withCredentials
property is an example where we do not want to overwrite whole method, but only add one header.I think that both
sendRequest
andhandleResponse
(they both should work the same way, otherwise our API will be inconsistent) should fire events onCKEDITOR
object with theloader
as a parameter.CKEDITOR
is better theneditor
instance because:CKEDITOR
instead of every instance.The only problem is that user should not change read-only loaders properties in handle response event, so they should be set as a event parameter and overwrite when the event is done (the same way evt.data.dataValue works).