Skip to content

Commit 2435caf

Browse files
committed
Adds a wpLoader factory function for creating loaders from the vue-router route object.
1 parent 5e479e6 commit 2435caf

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-wordpress",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "A collection of Vue components and mixins for use with WordPress and the WP-REST API",
55
"author": "WakeCoder <ajqajq@gmail.com>",
66
"scripts": {

src/example/example.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h1>vue-wordpress</h1>
44
<p>A collection of vue.js components and mixins for interfacing with the WordPress REST API. For information on how to use vue-wordpress, go to the github site. The most used and useful pieces of this repo are probably methods in the mixin:
55
<ul>
6-
<li>createWpLoader simplifies pulling data from the WP-REST API with postLoaders. PostLoaders support pagination and simplify async fetching of data. Support custom mappers.</li>
6+
<li>createWpLoader simplifies pulling data from the WP-REST API with WP loaders. WP loaders support pagination and simplify async fetching of data. Support custom mappers.</li>
77
<li>mapPosts is a default map function for use with createWpLoader to map the REST-API response for posts into a simpler data format.
88
<li>getTags is a function that can be used in the map function of a post loader to get all tags associated with a post.</li>
99
<li>getImageSource is a function that maps simple image sizes to WP-REST mapper properties</li>

src/mixins/wp-mixin.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ export default {
9797
return previous.concat(current.filter(t => t.taxonomy === tagTaxonomy)
9898
.map(t => t.slug))
9999
}, []) : []
100+
},
101+
/**
102+
* Takes a vue-router route object (this.$route) and returns the appropriate WP-REST API post loader.
103+
* This can be used with default (*) routing in the vue-router to dynamically pull in full posts.
104+
* Note: This makes the assumptions that your WP install is using slugs for permalinks
105+
* @param {object} options - {
106+
* {object} route: the vue-router route object (this.$route)
107+
* {string} baseUrl - Specifies the base URL for the WP-REST install (e.g. 'https://pixelthing.com/blog')
108+
* {string} postTypes - Optional. Can be used to specify a custom post type. Defaults to 'posts'
109+
* {array} queryParams - Optional. Can be used to specify additional query parameters for the wp loader
110+
* {bool} embed - Optional. Indicates whether links, tags, image URLs, etc. should be returned embedded in the WP REST response. Defaults to true.
111+
* {function} mapper - Optional. Used to map the WP-REST API response into a simpler object. Defaults to mapPosts
112+
*/
113+
createWpLoaderFromRoute({ route, baseUrl, type, queryParams, embed, mapper }) {
114+
let slug = route.params.slug || route.params[0]
115+
type = type || 'posts'
116+
const url = baseUrl + '/wp-json/wp/v2/' + type
117+
queryParams = queryParams || []
118+
queryParams.push('slug=' + slug)
119+
return this.createWpLoader(url, { queryParams, embed, mapper })
100120
}
101121
}
102122
}

0 commit comments

Comments
 (0)