{"action":"created","comment":{"url":"https://api.github.com/repos/WP-API/docs-v2/pulls/comments/50899222","id":50899222,"diff_hunk":"@@ -0,0 +1,183 @@\n+---\n+title: JavaScript Client\n+---\n+The REST API includes a JavaScript/Backbone client library.\n+\n+The library provides an interface for the WP REST API by providing Backbone Models and Collections for all endpoints exposed the API Schema.\n+\n+## Using\n+\n+Activate the WP-API plugin. Enqueue the script directly:\n+\n+```php\n+wp_enqueue_script( 'wp-api' );\n+```\n+\n+or as a dependency for your script:\n+\n+```php\n+wp_enqueue_script( 'my_script', 'path_to_my_script', array( 'wp-api' ) );\n+```\n+\n+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`.\n+\n+These objects contain the following:\n+\n+```\n+Models:\n+ * Categories\n+ * Comments\n+ * Media\n+ * Pages\n+ * PagesMeta\n+ * PagesRevisions\n+ * Posts\n+ * PostsCategories\n+ * PostsMeta\n+ * PostsRevisions\n+ * PostsTags\n+ * Schema\n+ * Statuses\n+ * Tags\n+ * Taxonomies\n+ * Types\n+ * Users\n+\n+Collections:\n+ * Categories\n+ * Comments\n+ * Customposttype\n+ * Media\n+ * Meta\n+ * Pages\n+ * Posts\n+ * Revisions\n+ * Statuses\n+ * Tags\n+ * Taxonomies\n+ * Types\n+ * Users\n+```\n+\n+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.\n+\n+### Default values\n+\n+Each model and collection includes a reference to its default values, for example:\n+\n+```\n+wp.api.models.Posts.defaults\n+ * author: null\n+ * comment_status: null\n+ * content: null\n+ * date: null\n+ * date_gmt: null\n+ * excerpt: null\n+ * featured_image: null\n+ * format: null\n+ * modified: null\n+ * modified_gmt: null\n+ * password: null\n+ * ping_status: null\n+ * slug: null\n+ * status: null\n+ * sticky: null\n+ * title: null\n+```\n+\n+### Available methods\n+\n+Each model and collection contains a list of methods the corrosponding endpoint supports. For example, models created from `wp.api.models.Posts` have a method array of:\n+\n+```\n+[\"GET\", \"POST\", \"PUT\", \"PATCH\", \"DELETE\"]\n+```\n+\n+### Accepted options\n+\n+Each model and collection contains a list of options the corrosponding endpoint accepts (passed as a second parameter), for example:\n+\n+```\n+wp.api.collections.Posts.options\n+ * author\n+ * context\n+ * filter\n+ * order\n+ * orderby\n+ * page\n+ * per_page\n+ * search\n+ * status\n+```\n+### Model example:\n+\n+To create a post and edit its categories, make sure you are logged in, then:\n+\n+```\n+// Create a new post\n+var post = new wp.api.models.Posts( { title: 'This is a test post' } );\n+post.save();\n+\n+// Create a new post\n+var post = new wp.api.models.Posts({ title:'new test' } );\n+post.save();\n+\n+// Get a collection of the post's categories\n+var postCategories = post.getCategories();\n+\n+// The new post has an single Category: Uncategorized\n+postCategories.at( 0 ).get('name');\n+// response -> \"Uncategorized\"\n+\n+// Set the post categories\n+post.setCategories( [ 'apples', 'oranges' ] );\n+\n+// Get all the categories\n+var allCategories = new wp.api.collections.Categories()\n+allCategories.fetch();\n+\n+var appleCategory = allCategories.findWhere( { slug: 'apples' } );\n+\n+// Add the category to the postCategories collection\n+appleCategory.set( 'parent_post', post.get( 'id' ) );\n+postCategories.create( appleCategory.toJSON(), { type: 'POST' } );\n+\n+// Remove the Uncategorized category\n+postCategories.at( 0 ).destroy();\n+\n+// Check the results - refectch\n+postCategories = post.getCategories();\n+\n+postCategories.at( 0 ).get('name');\n+// response -> \"apples\"\n+```\n+\n+### Collection examples:\n+\n+to get the last 10 posts:\n+\n+```\n+var postsCollection = new wp.api.collections.Posts();\n+postsCollection.fetch();\n+```\n+\n+to get the last 25 posts:\n+\n+```\n+postsCollection.fetch( { data: { per_page: 25 } } );\n+```\n+\n+use filter to change the order & orderby options:\n+\n+```\n+postsCollection.fetch( { data: { 'filter': { 'orderby': 'title', 'order': 'ASC' } } } );\n+```\n+\n+All collections support pagination automatically, and you can get the next page of results using `more`:\n+\n+```\n+postsCollection.more();\n+```\n+\n+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 that you may need to open a new tab to get a new read of the Schema.","path":"extending/javascript-client/index.md","position":182,"original_position":182,"commit_id":"bf63b5cd607d7cb5b84e0f1ea57544671f91d3a6","original_commit_id":"bf63b5cd607d7cb5b84e0f1ea57544671f91d3a6","user":{"login":"adamsilverstein","id":2676022,"avatar_url":"https://avatars.githubusercontent.com/u/2676022?v=3","gravatar_id":"","url":"https://api.github.com/users/adamsilverstein","html_url":"https://github.com/adamsilverstein","followers_url":"https://api.github.com/users/adamsilverstein/followers","following_url":"https://api.github.com/users/adamsilverstein/following{/other_user}","gists_url":"https://api.github.com/users/adamsilverstein/gists{/gist_id}","starred_url":"https://api.github.com/users/adamsilverstein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamsilverstein/subscriptions","organizations_url":"https://api.github.com/users/adamsilverstein/orgs","repos_url":"https://api.github.com/users/adamsilverstein/repos","events_url":"https://api.github.com/users/adamsilverstein/events{/privacy}","received_events_url":"https://api.github.com/users/adamsilverstein/received_events","type":"User","site_admin":false},"body":"yea, session cache. i'll make that clearer.","created_at":"2016-01-26T21:01:25Z","updated_at":"2016-01-26T21:01:25Z","html_url":"https://github.com/WP-API/docs-v2/pull/82#discussion_r50899222","pull_request_url":"https://api.github.com/repos/WP-API/docs-v2/pulls/82","_links":{"self":{"href":"https://api.github.com/repos/WP-API/docs-v2/pulls/comments/50899222"},"html":{"href":"https://github.com/WP-API/docs-v2/pull/82#discussion_r50899222"},"pull_request":{"href":"https://api.github.com/repos/WP-API/docs-v2/pulls/82"}}},"pull_request":{"url":"https://api.github.com/repos/WP-API/docs-v2/pulls/82","id":55052138,"html_url":"https://github.com/WP-API/docs-v2/pull/82","diff_url":"https://github.com/WP-API/docs-v2/pull/82.diff","patch_url":"https://github.com/WP-API/docs-v2/pull/82.patch","issue_url":"https://api.github.com/repos/WP-API/docs-v2/issues/82","number":82,"state":"open","locked":false,"title":"Add js client docs","user":{"login":"adamsilverstein","id":2676022,"avatar_url":"https://avatars.githubusercontent.com/u/2676022?v=3","gravatar_id":"","url":"https://api.github.com/users/adamsilverstein","html_url":"https://github.com/adamsilverstein","followers_url":"https://api.github.com/users/adamsilverstein/followers","following_url":"https://api.github.com/users/adamsilverstein/following{/other_user}","gists_url":"https://api.github.com/users/adamsilverstein/gists{/gist_id}","starred_url":"https://api.github.com/users/adamsilverstein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamsilverstein/subscriptions","organizations_url":"https://api.github.com/users/adamsilverstein/orgs","repos_url":"https://api.github.com/users/adamsilverstein/repos","events_url":"https://api.github.com/users/adamsilverstein/events{/privacy}","received_events_url":"https://api.github.com/users/adamsilverstein/received_events","type":"User","site_admin":false},"body":"* initial pass based on client-js repo readme","created_at":"2016-01-05T03:51:06Z","updated_at":"2016-01-26T21:01:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a918021d3519da81725a38505a162a3ff745df80","assignee":{"login":"adamsilverstein","id":2676022,"avatar_url":"https://avatars.githubusercontent.com/u/2676022?v=3","gravatar_id":"","url":"https://api.github.com/users/adamsilverstein","html_url":"https://github.com/adamsilverstein","followers_url":"https://api.github.com/users/adamsilverstein/followers","following_url":"https://api.github.com/users/adamsilverstein/following{/other_user}","gists_url":"https://api.github.com/users/adamsilverstein/gists{/gist_id}","starred_url":"https://api.github.com/users/adamsilverstein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamsilverstein/subscriptions","organizations_url":"https://api.github.com/users/adamsilverstein/orgs","repos_url":"https://api.github.com/users/adamsilverstein/repos","events_url":"https://api.github.com/users/adamsilverstein/events{/privacy}","received_events_url":"https://api.github.com/users/adamsilverstein/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/WP-API/docs-v2/pulls/82/commits","review_comments_url":"https://api.github.com/repos/WP-API/docs-v2/pulls/82/comments","review_comment_url":"https://api.github.com/repos/WP-API/docs-v2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/WP-API/docs-v2/issues/82/comments","statuses_url":"https://api.github.com/repos/WP-API/docs-v2/statuses/bf63b5cd607d7cb5b84e0f1ea57544671f91d3a6","head":{"label":"WP-API:add-js-client-docs","ref":"add-js-client-docs","sha":"bf63b5cd607d7cb5b84e0f1ea57544671f91d3a6","user":{"login":"WP-API","id":5706008,"avatar_url":"https://avatars.githubusercontent.com/u/5706008?v=3","gravatar_id":"","url":"https://api.github.com/users/WP-API","html_url":"https://github.com/WP-API","followers_url":"https://api.github.com/users/WP-API/followers","following_url":"https://api.github.com/users/WP-API/following{/other_user}","gists_url":"https://api.github.com/users/WP-API/gists{/gist_id}","starred_url":"https://api.github.com/users/WP-API/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WP-API/subscriptions","organizations_url":"https://api.github.com/users/WP-API/orgs","repos_url":"https://api.github.com/users/WP-API/repos","events_url":"https://api.github.com/users/WP-API/events{/privacy}","received_events_url":"https://api.github.com/users/WP-API/received_events","type":"Organization","site_admin":false},"repo":{"id":34363975,"name":"docs-v2","full_name":"WP-API/docs-v2","owner":{"login":"WP-API","id":5706008,"avatar_url":"https://avatars.githubusercontent.com/u/5706008?v=3","gravatar_id":"","url":"https://api.github.com/users/WP-API","html_url":"https://github.com/WP-API","followers_url":"https://api.github.com/users/WP-API/followers","following_url":"https://api.github.com/users/WP-API/following{/other_user}","gists_url":"https://api.github.com/users/WP-API/gists{/gist_id}","starred_url":"https://api.github.com/users/WP-API/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WP-API/subscriptions","organizations_url":"https://api.github.com/users/WP-API/orgs","repos_url":"https://api.github.com/users/WP-API/repos","events_url":"https://api.github.com/users/WP-API/events{/privacy}","received_events_url":"https://api.github.com/users/WP-API/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/WP-API/docs-v2","description":"Documentation for version 2 of the API","fork":false,"url":"https://api.github.com/repos/WP-API/docs-v2","forks_url":"https://api.github.com/repos/WP-API/docs-v2/forks","keys_url":"https://api.github.com/repos/WP-API/docs-v2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/WP-API/docs-v2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/WP-API/docs-v2/teams","hooks_url":"https://api.github.com/repos/WP-API/docs-v2/hooks","issue_events_url":"https://api.github.com/repos/WP-API/docs-v2/issues/events{/number}","events_url":"https://api.github.com/repos/WP-API/docs-v2/events","assignees_url":"https://api.github.com/repos/WP-API/docs-v2/assignees{/user}","branches_url":"https://api.github.com/repos/WP-API/docs-v2/branches{/branch}","tags_url":"https://api.github.com/repos/WP-API/docs-v2/tags","blobs_url":"https://api.github.com/repos/WP-API/docs-v2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/WP-API/docs-v2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/WP-API/docs-v2/git/refs{/sha}","trees_url":"https://api.github.com/repos/WP-API/docs-v2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/WP-API/docs-v2/statuses/{sha}","languages_url":"https://api.github.com/repos/WP-API/docs-v2/languages","stargazers_url":"https://api.github.com/repos/WP-API/docs-v2/stargazers","contributors_url":"https://api.github.com/repos/WP-API/docs-v2/contributors","subscribers_url":"https://api.github.com/repos/WP-API/docs-v2/subscribers","subscription_url":"https://api.github.com/repos/WP-API/docs-v2/subscription","commits_url":"https://api.github.com/repos/WP-API/docs-v2/commits{/sha}","git_commits_url":"https://api.github.com/repos/WP-API/docs-v2/git/commits{/sha}","comments_url":"https://api.github.com/repos/WP-API/docs-v2/comments{/number}","issue_comment_url":"https://api.github.com/repos/WP-API/docs-v2/issues/comments{/number}","contents_url":"https://api.github.com/repos/WP-API/docs-v2/contents/{+path}","compare_url":"https://api.github.com/repos/WP-API/docs-v2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/WP-API/docs-v2/merges","archive_url":"https://api.github.com/repos/WP-API/docs-v2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/WP-API/docs-v2/downloads","issues_url":"https://api.github.com/repos/WP-API/docs-v2/issues{/number}","pulls_url":"https://api.github.com/repos/WP-API/docs-v2/pulls{/number}","milestones_url":"https://api.github.com/repos/WP-API/docs-v2/milestones{/number}","notifications_url":"https://api.github.com/repos/WP-API/docs-v2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/WP-API/docs-v2/labels{/name}","releases_url":"https://api.github.com/repos/WP-API/docs-v2/releases{/id}","deployments_url":"https://api.github.com/repos/WP-API/docs-v2/deployments","created_at":"2015-04-22T02:28:11Z","updated_at":"2016-01-26T16:23:59Z","pushed_at":"2016-01-26T00:21:57Z","git_url":"git://github.com/WP-API/docs-v2.git","ssh_url":"git@github.com:WP-API/docs-v2.git","clone_url":"https://github.com/WP-API/docs-v2.git","svn_url":"https://github.com/WP-API/docs-v2","homepage":"http://v2.wp-api.org","size":317,"stargazers_count":24,"watchers_count":24,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":31,"mirror_url":null,"open_issues_count":30,"forks":31,"open_issues":30,"watchers":24,"default_branch":"gh-pages"}},"base":{"label":"WP-API:gh-pages","ref":"gh-pages","sha":"4ab7c0c26f998cce81c89d81ea8358f81c1bba85","user":{"login":"WP-API","id":5706008,"avatar_url":"https://avatars.githubusercontent.com/u/5706008?v=3","gravatar_id":"","url":"https://api.github.com/users/WP-API","html_url":"https://github.com/WP-API","followers_url":"https://api.github.com/users/WP-API/followers","following_url":"https://api.github.com/users/WP-API/following{/other_user}","gists_url":"https://api.github.com/users/WP-API/gists{/gist_id}","starred_url":"https://api.github.com/users/WP-API/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WP-API/subscriptions","organizations_url":"https://api.github.com/users/WP-API/orgs","repos_url":"https://api.github.com/users/WP-API/repos","events_url":"https://api.github.com/users/WP-API/events{/privacy}","received_events_url":"https://api.github.com/users/WP-API/received_events","type":"Organization","site_admin":false},"repo":{"id":34363975,"name":"docs-v2","full_name":"WP-API/docs-v2","owner":{"login":"WP-API","id":5706008,"avatar_url":"https://avatars.githubusercontent.com/u/5706008?v=3","gravatar_id":"","url":"https://api.github.com/users/WP-API","html_url":"https://github.com/WP-API","followers_url":"https://api.github.com/users/WP-API/followers","following_url":"https://api.github.com/users/WP-API/following{/other_user}","gists_url":"https://api.github.com/users/WP-API/gists{/gist_id}","starred_url":"https://api.github.com/users/WP-API/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WP-API/subscriptions","organizations_url":"https://api.github.com/users/WP-API/orgs","repos_url":"https://api.github.com/users/WP-API/repos","events_url":"https://api.github.com/users/WP-API/events{/privacy}","received_events_url":"https://api.github.com/users/WP-API/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/WP-API/docs-v2","description":"Documentation for version 2 of the API","fork":false,"url":"https://api.github.com/repos/WP-API/docs-v2","forks_url":"https://api.github.com/repos/WP-API/docs-v2/forks","keys_url":"https://api.github.com/repos/WP-API/docs-v2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/WP-API/docs-v2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/WP-API/docs-v2/teams","hooks_url":"https://api.github.com/repos/WP-API/docs-v2/hooks","issue_events_url":"https://api.github.com/repos/WP-API/docs-v2/issues/events{/number}","events_url":"https://api.github.com/repos/WP-API/docs-v2/events","assignees_url":"https://api.github.com/repos/WP-API/docs-v2/assignees{/user}","branches_url":"https://api.github.com/repos/WP-API/docs-v2/branches{/branch}","tags_url":"https://api.github.com/repos/WP-API/docs-v2/tags","blobs_url":"https://api.github.com/repos/WP-API/docs-v2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/WP-API/docs-v2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/WP-API/docs-v2/git/refs{/sha}","trees_url":"https://api.github.com/repos/WP-API/docs-v2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/WP-API/docs-v2/statuses/{sha}","languages_url":"https://api.github.com/repos/WP-API/docs-v2/languages","stargazers_url":"https://api.github.com/repos/WP-API/docs-v2/stargazers","contributors_url":"https://api.github.com/repos/WP-API/docs-v2/contributors","subscribers_url":"https://api.github.com/repos/WP-API/docs-v2/subscribers","subscription_url":"https://api.github.com/repos/WP-API/docs-v2/subscription","commits_url":"https://api.github.com/repos/WP-API/docs-v2/commits{/sha}","git_commits_url":"https://api.github.com/repos/WP-API/docs-v2/git/commits{/sha}","comments_url":"https://api.github.com/repos/WP-API/docs-v2/comments{/number}","issue_comment_url":"https://api.github.com/repos/WP-API/docs-v2/issues/comments{/number}","contents_url":"https://api.github.com/repos/WP-API/docs-v2/contents/{+path}","compare_url":"https://api.github.com/repos/WP-API/docs-v2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/WP-API/docs-v2/merges","archive_url":"https://api.github.com/repos/WP-API/docs-v2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/WP-API/docs-v2/downloads","issues_url":"https://api.github.com/repos/WP-API/docs-v2/issues{/number}","pulls_url":"https://api.github.com/repos/WP-API/docs-v2/pulls{/number}","milestones_url":"https://api.github.com/repos/WP-API/docs-v2/milestones{/number}","notifications_url":"https://api.github.com/repos/WP-API/docs-v2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/WP-API/docs-v2/labels{/name}","releases_url":"https://api.github.com/repos/WP-API/docs-v2/releases{/id}","deployments_url":"https://api.github.com/repos/WP-API/docs-v2/deployments","created_at":"2015-04-22T02:28:11Z","updated_at":"2016-01-26T16:23:59Z","pushed_at":"2016-01-26T00:21:57Z","git_url":"git://github.com/WP-API/docs-v2.git","ssh_url":"git@github.com:WP-API/docs-v2.git","clone_url":"https://github.com/WP-API/docs-v2.git","svn_url":"https://github.com/WP-API/docs-v2","homepage":"http://v2.wp-api.org","size":317,"stargazers_count":24,"watchers_count":24,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":31,"mirror_url":null,"open_issues_count":30,"forks":31,"open_issues":30,"watchers":24,"default_branch":"gh-pages"}},"_links":{"self":{"href":"https://api.github.com/repos/WP-API/docs-v2/pulls/82"},"html":{"href":"https://github.com/WP-API/docs-v2/pull/82"},"issue":{"href":"https://api.github.com/repos/WP-API/docs-v2/issues/82"},"comments":{"href":"https://api.github.com/repos/WP-API/docs-v2/issues/82/comments"},"review_comments":{"href":"https://api.github.com/repos/WP-API/docs-v2/pulls/82/comments"},"review_comment":{"href":"https://api.github.com/repos/WP-API/docs-v2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/WP-API/docs-v2/pulls/82/commits"},"statuses":{"href":"https://api.github.com/repos/WP-API/docs-v2/statuses/bf63b5cd607d7cb5b84e0f1ea57544671f91d3a6"}}}}