Skip to content
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

changed dustpress.js to class format and added eslint configs #23

Open
wants to merge 5 commits into
base: webpack
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": "wordpress",
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"no-unused-vars": [
"warn",
{
"args": "after-used",
"ignoreRestSiblings": false,
"vars": "all"
}
],
"prefer-const": [
"warn",
{
"destructuring": "any",
"ignoreReadBeforeAssign": false
}
],
"yoda": [
2,
"never"
]
}
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [3.0.5] - 2019-01-30

### Added
- webpack compiling for dustpress.js
- js sourcemaps
- .eslintrc.json

### Changed
- Changed the format of dustpress.js to a class

## [3.0.4] - 2018-08-09

Expand Down
180 changes: 180 additions & 0 deletions js/DPRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
export default class DPRequest {

/**
* Request path
*
* @type {String}
*/
path = '';

/**
* Default instance params
*
* @type {Object}
*/
params = {
get: '',
type: 'post',
tidy: false,
render: false,
partial: null,
upload: false,
data: false,
url: null,
bypassMainQuery: true,
contentType: 'application/json',
token: '',
success: () => {},
error: () => {},
uploadProgress: () => {},
downloadProgress: () => {},
start: () => {},
complete: () => {}
};

/**
* Construct the dustpress request
*
* @param {Object} params Params to use in request
*/
constructor( path, params = {}) {
this.path = path;

// Override default params with given ones
this.params = Object.assign( this.params, params );
}

send() {
if ( this.params.get.length && ! this.params.get.startsWith( '?' ) ) {
this.params.get = '?' + this.params.get;
}

const date = new Date();
date.setTime( date.getTime() + ( 24 * 60 * 60 * 1000 ) );

// Set the cookie for the token
document.cookie = 'dpjs_token=' + this.params.token + '; expires=' + date.toGMTString() + '; path=/';

const options = {
url: ( this.params.url || dustpressjs_endpoint || ( window.location + this.params.get ) ),
method: this.params.type,
contentType: this.params.contentType,
data: {
dustpress_data: {
path: this.path,
args: this.params.args,
render: this.params.render,
tidy: this.params.tidy,
partial: this.params.partial,
data: this.params.data,
token: this.params.token,
bypassMainQuery: this.params.bypassMainQuery
}
}
};

// Stringify data so it can be sent
options.data = JSON.stringify( options.data );

if ( this.params.upload ) {
options.xhr = () => this.xhrUpload();
}

this.params.start();

this.params.successHandler = ( data, textStatus, jqXHR ) => this.successHandler( data, textStatus, jqXHR );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of recreating these methods under the "params" key? I can't see the need for this since the methods are already accessible under the same scope. If you're thinking the class methods are not confined to a single object, I believe it is not the case. Each request instance should have its own methods which can be referenced safely for instance by assigning them as event handlers.

this.params.errorHandler = ( jqXHR, textStatus, errorThrown ) => this.errorHandler( jqXHR, textStatus, errorThrown );
this.params.uploadProgressHandler = ( event ) => this.uploadDownloadProgressHandler( event );
this.params.downloadProgressHandler = ( event ) => this.uploadDownloadProgressHandler( event );

return $.ajax( options )
.done( ( data, textStatus, jqXHR ) => this.params.successHandler( data, textStatus, jqXHR ) )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referring to my comment above, why can't this just be:

.done( ( data, textStatus, jqXHR ) => this.successHandler( data, textStatus, jqXHR ) )

this.successHandler here refers to a method of an instance since it is not static. Thus it should be the same thing as using this.params.successHandler.

.fail( ( jqXHR, textStatus, errorThrown ) => this.params.errorHandler( jqXHR, textStatus, errorThrown ) )
.always( ( dataOrXhr, textStatus, xhrOrError ) => this.params.complete( dataOrXhr, textStatus, xhrOrError ) );
}

/**
* Handle ajax success
*
* @param {string|mixed} data Data retrieved by ajax call.
* @param {string} textStatus Response status.
* @param {jqXHR} jqXHR Superset of the browser's native XMLHttpRequest object.
*/
successHandler( data, textStatus, jqXHR ) {
let parsed;

if ( typeof data === 'string' ) {
parsed = $.parseJSON( data );
} else {
parsed = data;
}

// Expire CSRF cookie
document.cookie = 'dpjs_token=; expires=-1; path=/';

// Add to debugger data if it exists
if ( window.DustPressDebugger ) {
this.addToDebugger( parsed );
}

if ( parsed && ! parsed.error ) {
this.params.success( parsed.success, parsed.data, textStatus, jqXHR );
} else {
this.params.error( parsed, textStatus, jqXHR );
}
}

/**
* Handle file uploads
*/
xhrUpload() {
const xhr = new window.XMLHttpRequest();

xhr.upload.addEventListener( 'progress', ( event ) => this.params.uploadProgressHandler( event ), false );
xhr.addEventListener( 'progress', ( event ) => this.params.uploadDownloadProgressHandler( event ), false );
}

/**
* Handle errors.
*
* @param {jqXHR} jqXHR Superset of the browser's native XMLHttpRequest object.
* @param {string} textStatus Response status.
* @param {mixed} errorThrown Ajax call error.
*/
errorHandler( jqXHR, textStatus, errorThrown ) {

// Expire CSRF cookie
document.cookie = 'dpjs_token=; expires=-1; path=/';

this.params.error({error: errorThrown}, textStatus, jqXHR );
}

/**
* Handle uploar or download progress.
*
* @param {object} event Event data.
*/
uploadDownloadProgressHandler( event ) {
if ( event.lengthComputable ) {
const complete = ( event.loaded / event.total );

this.params.downloadProgress( complete );
}
}

/**
* Add called data to dustpress debugger
*
* @param {object} parsed Parsed data.
*/
addToDebugger( parsed ) {
delete this.params.params.success;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where could these come from and why are they removed here?

delete this.params.params.error;

const debug = {
params: this.params.params,
data: parsed.debug ? parsed.debug : parsed
};
window.DustPressDebugger.extend( debug, this.params.path );
}
}
2 changes: 1 addition & 1 deletion js/dustpress-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading