Skip to content

A DustPress plugin that provides a handy JavaScript library for using your DustPress Model methods on the front end.

License

Notifications You must be signed in to change notification settings

godbone/dustpress-js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

geniem-github-banner

DustPress Plugin: DustPress.js

A DustPress plugin that provides a handy JavaScript library for using your DustPress model methods on the front end.

Usage

You can call for SomeModel's method "SomeMethod" with the following call:

dp("SomeModel/SomeMethod", {
	tidy: true,
	success: function( data ) {
		// do what you want with the data
	},
	error: function( 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.

If you want, you can even render HTML with Dust templates.

dp("SomeModel/SomeMethod", {
	partial: "SomePartial",
	success: function( data ) {
		// do what you want with the data
	},
	error: function( error ) {
		// possible error
	}
});

This code takes the data of SomeMethod and renders it with SomePartial. Variable data then contains the ready html.

You can also omit the method completely if you want to get the data of a complete model.

dp("SomeModel", {
	success: function( data ) {
		// do what you want with the data
	},
	error: function( error ) {
		// possible error
	}
});

If you want to call several functions but not all at once, you can do so by replacing the method's name on the call with a comma-separated list.

dp("SomeModel/SomeMethod,AnotherMethod", {
	success: function( data ) {
		// data.SomeMethod and data.AnotherMethod contain the return values
	},
	error: function( error ) {
		// possible error
	}
});

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.

Model function front-end visibility

All public functions in your DustPress models are accessible via AJAX by default. You can also call protected methods from the models by defining a protected array property with the name $api to your model with a list of method names that should be allowed to be run via AJAX.

class SomeModel extends DustPressModel {
    protected $api = [
        'SomeMethod'
    ];

    protected function SomeMethod() {
        // Some code..
    }
}

If you need to block visibility of public funcions, you can do this by examining the DOING_AJAX constant or calling the core function is_dustpress_ajax.

class SomeModel extends DustPressModel {
    protected $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 WP project is through composer:

$ composer require devgeniem/dustpress-js

About

A DustPress plugin that provides a handy JavaScript library for using your DustPress Model methods on the front end.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 76.6%
  • PHP 23.4%