Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 5, 2018. It is now read-only.
/client-jsPublic archive

Commit5cccffb

Browse files
Merge branch 'update-readme'
2 parentsc1825ea +fa88174 commit5cccffb

File tree

1 file changed

+3
-269
lines changed

1 file changed

+3
-269
lines changed

‎README.md‎

Lines changed: 3 additions & 269 deletions
Original file line numberDiff line numberDiff line change
@@ -1,274 +1,8 @@
11
Backbone library for the WordPress REST API or "WP-API"
22
==============
33

4-
##Summary
4+
##Note:
55

6-
Thislibrary provides an interface for the[WP REST API](https://github.com/WP-API/WP-API) by providing Backbone ModelsandCollections for all endpoints in the API.
6+
Thisclient is now bundled in WordPress coreanddevelopment is happening there, seehttps://trac.wordpress.org.
77

8-
9-
##Using
10-
11-
Activate the WP-API plugin. Enqueue the script directly:
12-
13-
```php
14-
wp_enqueue_script( 'wp-api' );
15-
```
16-
17-
or as a dependency for your script:
18-
19-
```php
20-
wp_enqueue_script( 'my_script', 'path/to/my/script', array( 'wp-api' ) );
21-
```
22-
23-
The library parses the root endpoint (the 'Schema') and creates matching Backbone models and collections. You will now have two root objects available to you:`wp.api.models` and`wp.api.collections`.
24-
25-
The models and collections include:
26-
27-
```
28-
Models:
29-
* Category
30-
* Comment
31-
* Media
32-
* Page
33-
* PageMeta
34-
* PageRevision
35-
* Post
36-
* PostMeta
37-
* PostRevision
38-
* Schema
39-
* Status
40-
* Tag
41-
* Taxonomy
42-
* Type
43-
* User
44-
45-
Collections:
46-
* Categories
47-
* Comments
48-
* Media
49-
* PageMeta
50-
* PageRevisions
51-
* Pages
52-
* Posts
53-
* Statuses
54-
* Tags
55-
* Taxonomies
56-
* Types
57-
* Users
58-
```
59-
60-
You can use these endpoints as-is to read, update, create and delete items using standard Backbone methods (fetch, sync, save & destroy for models, sync for collections). You can also extend these objects to make them your own, and build your views on top of them.
61-
62-
###Default values
63-
64-
Each model and collection includes a reference to its default values, for example:
65-
66-
```
67-
wp.api.models.Post.prototype.args
68-
* author: null
69-
* comment_status: null
70-
* content: null
71-
* date: null
72-
* date_gmt: null
73-
* excerpt: null
74-
* featured_image: null
75-
* format: null
76-
* modified: null
77-
* modified_gmt: null
78-
* password: null
79-
* ping_status: null
80-
* slug: null
81-
* status: null
82-
* sticky: null
83-
* title: null
84-
```
85-
86-
###Available methods
87-
88-
Each model and collection contains a list of methods the corresponding endpoint supports. For example, models created from`wp.api.models.Post` have a methods array of:
89-
90-
```
91-
["GET", "POST", "PUT", "PATCH", "DELETE"]
92-
```
93-
94-
###Accepted options
95-
96-
Each model and collection contains a list of options the corrosponding endpoint accepts (note that options are passed as the second parameter when creating models or collections), for example:
97-
98-
```
99-
wp.api.collections.Posts.prototype.options
100-
* author
101-
* context
102-
* filter
103-
* order
104-
* orderby
105-
* page
106-
* per_page
107-
* search
108-
* status
109-
```
110-
111-
###Localizing the API Schema
112-
The client will accept and use a localized schema as part of the`wpApiSettings` object. The Schema is currently not passed by default; instead the client makes an ajax request to the API to load the Schema, then caches it in the browser's session storage (if available). Activating the client-js plugin with`SCRIPT_DEBUG` enabled uses a localized Schema. Check the[client-js example](https://github.com/WP-API/client-js/blob/master/client-js.php) or this branch which[attempts to only localize the schema once per client](https://github.com/WP-API/client-js/compare/features/only-localize-schma-once?expand=1).
113-
114-
###Waiting for the client to load
115-
Client startup is asynchronous. If the api schema is localized, the client can start immediately; if not the client makes an ajax request to load the schema. The client exposes a load promise for provide a reliable wait to wait for client to be ready:
116-
117-
```js
118-
wp.api.loadPromise.done(function() {
119-
//... use the client here
120-
} )
121-
```
122-
123-
###Model examples:
124-
125-
To create a post and edit its categories, make sure you are logged in, then:
126-
127-
```js
128-
// Create a new post
129-
var post=newwp.api.models.Post( { title:'This is a test post' } );
130-
post.save();
131-
132-
// Load an existing post
133-
var post=newwp.api.models.Post( { id:1 } );
134-
post.fetch();
135-
136-
// Get a collection of the post's categories (returns a promise)
137-
// Uses _embedded data if available, in which case promise resolves immediately.
138-
post.getCategories().done(function(postCategories ) {
139-
// ... do something with the categories.
140-
// The new post has an single Category: Uncategorized
141-
console.log( postCategories[0].name );
142-
// response -> "Uncategorized"
143-
} );
144-
145-
// Get a posts author User model.
146-
post.getAuthorUser().done(function(user ){
147-
// ... do something with user
148-
console.log(user.get('name' ) );
149-
} );
150-
151-
// Get a posts featured image Media model.
152-
post.getFeaturedImage().done(function(image ){
153-
// ... do something with image
154-
console.log( image );
155-
} );
156-
157-
// Set the post categories.
158-
post.setCategories( ['apples','oranges' ] );
159-
160-
// Get all the categories
161-
var allCategories=newwp.api.collections.Categories()
162-
allCategories.fetch();
163-
164-
var appleCategory=allCategories.findWhere( { slug:'apples' } );
165-
166-
// Add the category to the postCategories collection we previously fetched.
167-
appleCategory.set('parent_post',post.get('id' ) );
168-
169-
// Use the POST method so Backbone will not PUT it even though it has an id.
170-
postCategories.create(appleCategory.toJSON(), { type:'POST' } );
171-
172-
// Remove the Uncategorized category
173-
postCategories.at(0 ).destroy();
174-
175-
// Check the results - re-fetch
176-
postCategories=post.getCategories();
177-
178-
postCategories.at(0 ).get('name' );
179-
// response -> "apples"
180-
```
181-
182-
###Collection examples:
183-
184-
to get the last 10 posts:
185-
186-
```js
187-
var postsCollection=newwp.api.collections.Posts();
188-
postsCollection.fetch();
189-
```
190-
191-
to get the last 25 posts:
192-
193-
```js
194-
postsCollection.fetch( { data: { per_page:25 } } );
195-
```
196-
197-
use filter to change the order & orderby options:
198-
199-
```js
200-
postsCollection.fetch( { data: {'filter': {'orderby':'title','order':'ASC' } } } );
201-
```
202-
203-
All collections support pagination automatically, and you can get the next page of results using`more`:
204-
205-
```js
206-
postsCollection.more();
207-
```
208-
209-
to get page 5 of a collection:
210-
211-
```js
212-
posts.fetch( { data: { page:5 } } );
213-
```
214-
215-
check if the collection has any more posts:
216-
217-
```js
218-
posts.hasMore();
219-
```
220-
221-
###Working With Revisions
222-
You can access post or page revisions using the PostRevisions or PageRevisions collections or through the Post or Page collection.
223-
224-
For example, to get a collection of all revisions of post ID 1:
225-
```
226-
var revisions = new wp.api.collections.PostRevisions({}, { parent: 1 });
227-
```
228-
229-
Revision collections can also be accessed via their parent's collection. This example makes 2 HTTP requests instead of one, but now the original post and its revisions are avaible:
230-
231-
```
232-
var post = new wp.api.models.Post( { id: 1 } );
233-
post.fetch();
234-
post.getRevisions().done( function( revisions ){
235-
console.log( revisions );
236-
});
237-
```
238-
239-
If you add custom endpoints to the api they will also become available as models/collections. For example, you will get new models and collections when you[add REST API support to your custom post type](http://v2.wp-api.org/extending/custom-content-types/). Note: because the schema is stored in the user's session cache to avoid re-fetching, you may need to open a new tab to get a new read of the Schema.
240-
241-
##Development
242-
243-
To develop, build and test this library, you must have[Node](http://nodejs.org) installed. For Windows users, simply[download](http://nodejs.org/download/) and install Node. For Mac users, we recommend installing Node using[Homebrew](http://mxcl.github.com/homebrew/). Once Homebrew is installed, run`brew install node` to install Node.js.
244-
245-
###Installation
246-
247-
Clone this repository, and then execute the following commands within the checkout directory:
248-
```bash
249-
$ npm install
250-
```
251-
This will use Node's NPM package manager to install all the dependencies for building and testing this library. We use[Bower](http://bower.io) to manage client script dependencies, but Bower script installation is handled as part of the`npm install` command.
252-
253-
###Building
254-
255-
To update the compiled JavaScript files in the`build/` directory after you've made changes, run the library's`build` script with the npm command:
256-
```bash
257-
$ npm run build
258-
```
259-
This will use[Grunt](http://gruntjs.com) to check the source scripts in`js/` for syntax errors, then concatenate and minify them to create[the minified wp-api.min.js file](build/js/wp-api.min.js) and a corresponding source map file.
260-
261-
###Testing
262-
263-
You can run the unit tests for this library using Grunt:
264-
```bash
265-
$ npmtest
266-
```
267-
268-
####A note on Grunt
269-
270-
The custom "build" and "test" scripts defined in this library's[package.json](package.json) enable access to Grunt's functionality after a simple`npm install`; however, these commands can also be run directly using Grunt itself. In order to gain access to the`grunt` console command, you must globally install the Grunt command-line interface:
271-
```bash
272-
$ npm install -g grunt-cli
273-
```
274-
Once`grunt-cli` has been installed, you can run the build and test commands with`grunt` and`grunt test`, respectively, without having to invoke the scripts via NPM.
8+
Documentation has moved to the WordPress handbook:https://developer.wordpress.org/rest-api/using-the-rest-api/backbone-javascript-client/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp