Ember Wordpress is an addon for ember-cli that makes it easy to fetch data from the Wordpress API (WP-API) in your Ember sites. It includes an application adapter, serializer as well as some default models: post, page, category and tag.
Note, the demo API sometimes goes to sleep. Please open an issue if so.
A few sites using ember-wordpress:
ember install ember-wordpress
// config/environment.js
...
ENV.emberWordpress: {
host: 'https://my-wordpress-site.com'
}
On a version before 2.0.1? Use ENV.wordpressHost
instead.
You'll have seven models ready out of the box: wordpress/post
, wordpress/page
, wordpress/category
wordpress/tag
, wordpress/attachment
, wordpress/comment
, wordpress/user
.
Note: the wordpress/post
and wordpress/page
models are identical and so are wordpress/category
and wordpress/tag
. For your own custom post types, it is recommended to extend the post
model:
// app/models/recipe.js
import DS from 'ember-data';
import PostModel from 'ember-wordpress/models/post';
export default PostModel.extend({
ingredients: DS.attr()
});
If you're using the ACF plugin your custom fields will be at model.get('acf.myCustomField')
.
Since Wordpress version 4.7 the REST API is included in core Wordpress. If you are on an earlier version you will need to install the WP API v2 plugin, which also works fine.
After installing, create some posts or pages in Wordpress and see your data at example.com/wp-json/wp/v2
.
If you're having CORS trouble: WP-CORSIf you want custom fields: Advanced Custom Fields and ACF To REST API
To use a custom post type together with the WP API you have to be aware of two additional arguments, when you define them.
show_in_rest
must be set to true.rest_base
will be the endpoint of your post type. Sset it to the plural form of your model, as this is what Ember expects. E.g. the endpoint for a recipe
post type should be recipies
and not recipe
.Here's a full example. You could save this file as wp-content/plugins/my-custom-post-types.php
.
<?php
/*
Plugin Name: My custom post types
Author URI: https://github.com/oskarrough/ember-wordpress/
*/
function artist_post_type() {
$labels = array(
'name' => 'Artists',
'singular_name' => 'Artist',
'menu_name' => 'Artists',
);
$args = array(
'labels' => $labels,
'show_in_rest' => true,
'rest_base' => 'artists',
);
register_post_type('artist', $args);
}
add_action('init', 'artist_post_type');
?>
The WP API supports many arguments that you can use but it's not super friendly so here are some tips.
By default the WP API returns a maximum of 10 items. For instance, this.store.findAll('post')
would return 10 posts. To change that we need to find the right argument and endpoint. Looking at the documentation for WP API we find per_page
. It could look like wp-json/wp/v2/posts?per_page=99
which translates into the ember-data query this.store.query('post', {per_page: 99})
.
wp-json/wp/v2/posts?slug=some-post-slug
this.store.query('post', {slug: 'some-post-plug'}).then(models => models.get('firstObject'));
We take the first object because query
always returns an array and we expect our query to only return a single object.
To query posts by category slug you will need two queries.
First get the category id with the
wp-json/wp/v2/categories?slug=some-category-slug
this.store.query('category', {slug: 'some-category-slug'}).then(models => models.get('firstObject'));
Then, get the posts
wp-json/wp/v2/posts?categories=category-id&per_page=99
this.store.query('post', {per_page: 99, categories: category-id}).then(models => models.get('firstObject'));
Enable caching by installing the wp-rest-api-cache wordpress plugin.
To get server-side rendering, install Ember Fastboot. Here's a demo of the Ember Wordpress dummy app served by fastboot. You'll see the actual HTML rendered if you view the source. Ember Wordpress doesn't require anything special to make this work. Here's a small deployment tip.
By default, Ember loads every request to a record separately from the server. If you want to display a post and the names of all of it's tags for example, Ember will query the main post and every single tag. A post with five tags will result in six requests to the server.
Since Ember and WP-API supports loading of multiple resources of the same type in one request, you can opt-in to this feature:
var ENV = {
...
emberWordpress: {
coalesceFindRequests: true
}
...
With this option enabled, loading a post with five tags will result in just two requests, because all tags of the post will be loaded together. This can improve the load time of your Ember app a lot!
It's the goal of ember-wordpress to be the bridge between ember/ember-data and the official WP REST API. Ideally, in addition to the provided adapter, serializer and models, this readme and the project's demo app should serve as good examples. Please ask any questions here https://github.com/oskarrough/ember-wordpress/issues.
我正在尝试从两个类别(新闻和事件)获得两个博客帖子列表,然后将它们显示在我的主页的两个不同的列中.我需要执行2个单独的Ajax调用才能获得这些博文.我不使用ember-data进行这个操作,因为我在这种情况下看不到使用它的优点(但是我可能是错误的). export default Ember.Route.extend({ setupController(controller,model) { va
Ember检查器是一个浏览器插件,用于调试Ember应用程序。 灰烬检查员包括以下主题 - S.No. 灰烬检查员方式和描述 1 安装Inspector 您可以安装Ember检查器来调试您的应用程序。 2 Object Inspector Ember检查器允许与Ember对象进行交互。 3 The View Tree 视图树提供应用程序的当前状态。 4 检查路由,数据选项卡和库信息 您可以看到检查
英文原文: http://emberjs.com/guides/getting-ember/index/ Ember构建 Ember的发布管理团队针对Ember和Ember Data维护了不同的发布方法。 频道 最新的Ember和Ember Data的 Release,Beta 和 Canary 构建可以在这里找到。每一个频道都提供了一个开发版、最小化版和生产版。更多关于不同频道的信息可以查看博客
ember-emojione ember-emojione is your emoji solution for Ember, based on the EmojiOne project. EmojiOne version 2 is used, which is free to use for everyone (CC BY-SA 4.0), you're only required to giv
Ember 3D Ember 3D is an Ember addon for using Three.js - an easy to use, lightweight, javascript 3D library. It is designed to: Prescribe a solid file structure to Three.js code using ES6 modules. Ena
Ember Table An addon to support large data set and a number of features around table. Ember Table canhandle over 100,000 rows without any rendering or performance issues. Ember Table 3.x supports: Emb
vscode-ember This is the VSCode extension to use the Ember Language Server. Features All features currently only work in Ember-CLI apps that use classic structure and are a rough first draft with a lo
ember-headlessui This is a work-in-progress implementation of: https://github.com/tailwindlabs/headlessui A set of completely unstyled, fully accessible UI components for Ember.js, designed to integra
Ember Popper An Ember-centric wrapper around Popper.js. Currently an alpha in active development. See the dummy app for examples Compatibility Ember.js v3.12 or above Ember CLI v2.13 or above Node.js