devgeniem / dustpress-js
A DustPress plugin that provides a handy JavaScript library for using your DustPress Model methods on the front end.
Installs: 54 839
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 31
Forks: 2
Open Issues: 12
Language:JavaScript
Type:wordpress-plugin
Requires
- php: >=7.0
- composer/installers: ^1||^2
- devgeniem/dustpress: >=1.25.0
- dev-master
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.1
- 4.1.0
- 4.0.1
- 4.0.0
- 4.0.0-beta
- v3.x-dev
- 3.1.1
- 3.1.0
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.3.3
- 2.3.2
- v2.3.1.x-dev
- 2.3.1
- 2.3.0
- v2.2.0.x-dev
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.3.x-dev
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.0
- dev-dependabot/npm_and_yarn/json5-1.0.2
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/loader-utils-and-webpack-cli-1.4.2
- dev-dependabot/npm_and_yarn/terser-4.8.1
- dev-dependabot/npm_and_yarn/ajv-6.12.6
- dev-dependabot/npm_and_yarn/path-parse-1.0.7
- dev-210618-error-handling-fix
- dev-php8
- dev-redirect-fix
- dev-fetch-retry
- dev-fix-ajax-get-request
- dev-hotfix-filter-dustpressjs-endpoint
- dev-jquery-to-footer
- dev-add-data-property
- dev-hotfix-191108
- dev-fetch
- dev-class-format
- dev-webpack
- dev-error-fix
- dev-ajax-fix
- dev-hotfix-180627-b
- dev-hotfix-180627
- dev-bypassmainquery
- dev-hotfix-180525
- dev-data-parameter
- dev-fix-171115
- dev-enqueue-version-fix
- dev-ajax-json
- dev-parse_json_error
- dev-js_version_bump
This package is auto-updated.
Last update: 2024-11-13 15:07:19 UTC
README
DustPress Plugin: DustPress.js
A DustPress plugin that provides a handy JavaScript library for using your DustPress model methods on the front end.
- Contributors: devgeniem / Nomafin, villesiltala, godbone
- Plugin url: https://github.com/devgeniem/dustpress-debugger
- Tags: dustpress, wordpress, plugins, dustjs, dust.js
- Requires at least: 4.9.0
- Requires DustPress version: 1.25.0
- Tested up to: 5.2.0
- License: GPL-3.0
- License URI: http://www.gnu.org/licenses/gpl-3.0.html
Usage
You can call for SomeModel
's method SomeMethod
with following code:
dp( 'SomeModel/SomeMethod', { tidy: true, args: { 'foo': 'bar' } }).then( ( data ) => { // do what you want with the data }).catch( ( error ) => { // possible error });
tidy: true
parameter cleans up the data tree a bit for more usability. Feel free to try the queries with and without it to see the difference.
args
parameter passes arguments to the PHP side. They can be accessed there with $this->get_args();
.
If you want, you can even render HTML with Dust templates.
dp( 'SomeModel/SomeMethod', { partial: 'SomePartial', }).then( ( data ) => { // do what you want with the data }).catch( ( error ) => { // possible error });
This code takes the data of SomeMethod
and renders it with SomePartial
. Variable data
then contains the ready html.
If you still want to get the data output as well, use argument data: true
and you will get the resulting data as the second parameter of your success function.
You can also omit the method completely if you want to get the data of a complete model.
dp( 'SomeModel/SomeMethod' ).then( ( data ) => { // do what you want with the data }).catch( ( error ) => { // possible error });
It is also possible to use the dp
call with the async-await pattern:
try { var foobar = await dp( 'SomeModel/SomeMethod', { tidy: true, args: { foo: 'bar' } }); } catch( err ) { console.error( err ); }
Now data will consist of an object with the methods' names as keys and their return values as the values. Obviously you can also render that to HTML as well.
Additional parameters
bypassMainQuery
By default DustPress.js requests bypass WordPress' main WP_Query so that it wouldn't slow the request down when it's not necessary. You can prevent that from happening by setting bypassMainQuery: false
if you want to use the default query.
Model function front-end visibility
You need to make your methods accessible for DustPress.js by defining a property named $api
in your model. It should be an array consisting of the names of your accessible methods. DustPress.js can also run protected
methods which are not automatically run by DustPress normally.
class SomeModel extends DustPressModel {
public $api = [
'SomeMethod'
];
protected function SomeMethod() {
// Some code..
}
}
If you need to determine whether a call has been made from ajax or not, you can use dustpress()->is_dustpress_ajax()
function like shown below:
class SomeModel extends DustPressModel {
public $api = [
'PublicMethod'
];
public function PublicMethod() {
if ( dustpress()->is_dustpress_ajax() ) {
// Do not run if this an ajax request.
return;
}
// Some code..
}
}
Install
Recommended installation to a WordPress project is through composer:
$ composer require devgeniem/dustpress-js
Obviously you can also download the ZIP file from GitHub and extract it into your plugins directory.