1.0.0 • Published 6 years ago

oc-request v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

OC-request is vanilla-js based replacement for standard OctoberCMS api. No dependencies. Based on native XMLHttpRequest.

Contents

Introduction

In modern frontend development, the use of jQuery is less and less encouraged and there are a number of reasons for this. Here are a few of them: 1. Most of the jQuery functionality is easy to write on pure JS. 2. jQuery greatly increases the size of the bundle, with most of the functionality is not used. 3. jQuery encourages writing bad and unreliable code.

On most of my recent projects, I had to add jquery to the bundle just because of the request() function, so I decided that it was time to remove this vestige. I tried to keep the structure of request creation and response processing as similar as possible to current api. The only thing that has not been implemented is the part of the api that is based on 'data-' attributes. I find this functionality unnecessary and vulnerable due to the use of the eval() method.

Installation

NPM

npm install oc-request --save

Usage exmaple:

import request from 'oc-request';

// Send some information
request.sendData('ProductList::onAjaxRequest', {
    data: {
      сategory_id: id,
    },
      update: { 'product/slider/slider-ajax': '.slider-ajax-wrapper' },
      success: () => {
      doSomething();
    },
      complete: () => {
      doSomethingAfterSuccess();
    },
    loading: '.preloader-selector',
});

// Send form
const formNode = document.querySelector('.my-form');

request.sendForm(form, 'MakeReview::onCreate', {
   success: (res) => {
      completeHandler(res);
   },
});

Methods

There are two main methods for request object: 1. sendData(handler, options); 2. sendForm(form, handler, options).

The set of options is standard for both and very similar to standard OctoberCMS api

OptionsDefaultDescription
datanullan optional object specifying data to be sent to the server along with the form data: {var: 'value'}.
updatenullan object, specifies a list partials and page elements (as CSS selectors) to update: {'partial': '#select'}. If the selector string is prepended with the @ symbol, the content received from the server will be appended to the element at begin or if string contains '^' symbol - at the end, instead of replacing the existing content.
flashfalsewhen true, instructs the server to clear and send any flash messages with the response.
filesfalsewhen true, the request will accept file uploads, this requires FormData interface support by the browser.
loadingnullan optional string or object to be displayed when a request runs. The string should be a CSS selector for an element, the object should support the show() and hide() functions to manage the visibility.
redirecttruestring specifying an URL to redirect the browser to after the successful request.
successnullUnlike the standard api, this is the primary method that runs when the query succeeds and runs AFTER the update () function. As a parameter, it takes ONE parameter - an object with a response from the server.
errornullthe callback function is executed in case of an error. Accepts error text as a parameter
completenullExecuted AFTER success or error methods

Tips for easy migration

  1. For event delegation there is very useful library from github developers.
  2. You may to use axios or fetch for request to custom request to server.
  3. This package is designed for the latest browsers. For better compatibility don't forget to add it for Babel processing.

Version history

  • 0.1.3 - Added basic functionality for sending data and updating DOM
  • 0.2.0 - Migrate from axios to native XMLHttpRequest
  • 0.2.3 - Add methods for manual files sending
  • 0.3.0 - Add methods for form serialize and send
  • 0.9.0 - Add documentation
  • 1.0.0 - Release
1.0.0

6 years ago

0.9.6

6 years ago

0.9.5

6 years ago

0.9.4

6 years ago

0.9.3

6 years ago

0.9.2

6 years ago

0.9.1

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago