devgeniem / dustpress-js
DustPress 插件,提供了一款便捷的 JavaScript 库,用于在前端使用 DustPress 模型方法。
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-09-13 14:52:31 UTC
README
DustPress 插件:DustPress.js
DustPress 插件,提供了一款便捷的 JavaScript 库,用于在前端使用 DustPress 模型方法。
- 贡献者: devgeniem / Nomafin, villesiltala, godbone
- 插件链接: https://github.com/devgeniem/dustpress-debugger
- 标签: dustpress, wordpress, plugins, dustjs, dust.js
- 最低要求:4.9.0
- 要求 DustPress 版本:1.25.0
- 测试至:5.2.0
- 许可:GPL-3.0
- 许可 URI: https://gnu.ac.cn/licenses/gpl-3.0.html
用法
您可以使用以下代码调用 SomeModel
的方法 SomeMethod
dp( 'SomeModel/SomeMethod', { tidy: true, args: { 'foo': 'bar' } }).then( ( data ) => { // do what you want with the data }).catch( ( error ) => { // possible error });
tidy: true
参数对数据树进行一些清理,使其更易于使用。您可以尝试带有和不带有它的查询以查看差异。
args
参数将参数传递到 PHP 侧。您可以使用 $this->get_args();
在那里访问它们。
如果您愿意,您甚至可以使用 Dust 模板渲染 HTML。
dp( 'SomeModel/SomeMethod', { partial: 'SomePartial', }).then( ( data ) => { // do what you want with the data }).catch( ( error ) => { // possible error });
此代码使用 SomeMethod
的数据,并用 SomePartial
进行渲染。变量 data
然后包含准备好的 HTML。
如果您还想获取数据输出,请使用参数 data: true
,您将在成功函数的第二个参数中获得结果数据。
如果您想获取完整模型的数据,也可以完全省略方法。
dp( 'SomeModel/SomeMethod' ).then( ( data ) => { // do what you want with the data }).catch( ( error ) => { // possible error });
还可以使用异步-等待模式使用 dp
调用
try { var foobar = await dp( 'SomeModel/SomeMethod', { tidy: true, args: { foo: 'bar' } }); } catch( err ) { console.error( err ); }
现在数据将是一个对象,其中方法的名称作为键,其返回值作为值。显然,您也可以将其渲染为 HTML。
附加参数
bypassMainQuery
默认情况下,DustPress.js 会绕过 WordPress 的主 WP_Query,以免在不必要的情况下减慢请求速度。如果您想使用默认查询,可以设置 bypassMainQuery: false
。
模型函数前端可见性
您需要通过在您的模型中定义一个名为 $api
的属性来使 DustPress.js 可访问您的方法。它应该是一个数组,包含您可访问的方法名称。DustPress.js 还可以运行 DustPress 通常不会自动运行的 protected
方法。
class SomeModel extends DustPressModel {
public $api = [
'SomeMethod'
];
protected function SomeMethod() {
// Some code..
}
}
如果您需要确定是否已从 AJAX 调用中调用,可以使用以下示例中的 dustpress()->is_dustpress_ajax()
函数
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..
}
}
安装
推荐通过 composer 安装到 WordPress 项目中
$ composer require devgeniem/dustpress-js
显然,您也可以从 GitHub 下载 ZIP 文件并将其解压到您的插件目录中。