PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/json-rest-api/wp-api.js

https://gitlab.com/dhanukanuwan/hangover-office
JavaScript | 987 lines | 581 code | 141 blank | 265 comment | 60 complexity | 7a14b7671a3853db5baa248b1cf63a6c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. (function( window, undefined ) {
  2. 'use strict';
  3. function WP_API() {
  4. this.models = {};
  5. this.collections = {};
  6. this.views = {};
  7. }
  8. window.wp = window.wp || {};
  9. wp.api = wp.api || new WP_API();
  10. })( window );
  11. (function( Backbone, _, window, undefined ) {
  12. //'use strict';
  13. // ECMAScript 5 shim, from MDN
  14. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
  15. if ( ! Date.prototype.toISOString ) {
  16. var pad = function( number ) {
  17. var r = String( number );
  18. if ( r.length === 1 ) {
  19. r = '0' + r;
  20. }
  21. return r;
  22. };
  23. Date.prototype.toISOString = function() {
  24. return this.getUTCFullYear() +
  25. '-' + pad( this.getUTCMonth() + 1 ) +
  26. '-' + pad( this.getUTCDate() ) +
  27. 'T' + pad( this.getUTCHours() ) +
  28. ':' + pad( this.getUTCMinutes() ) +
  29. ':' + pad( this.getUTCSeconds() ) +
  30. '.' + String( ( this.getUTCMilliseconds()/1000 ).toFixed( 3 ) ).slice( 2, 5 ) +
  31. 'Z';
  32. };
  33. }
  34. function WP_API_Utils() {
  35. var origParse = Date.parse,
  36. numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
  37. this.parseISO8601 = function( date ) {
  38. var timestamp, struct, i, k,
  39. minutesOffset = 0;
  40. // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
  41. // before falling back to any implementation-specific date parsing, so that’s what we do, even if native
  42. // implementations could be faster
  43. // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
  44. if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
  45. // avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
  46. for ( i = 0; ( k = numericKeys[i] ); ++i) {
  47. struct[k] = +struct[k] || 0;
  48. }
  49. // allow undefined days and months
  50. struct[2] = ( +struct[2] || 1 ) - 1;
  51. struct[3] = +struct[3] || 1;
  52. if ( struct[8] !== 'Z' && struct[9] !== undefined ) {
  53. minutesOffset = struct[10] * 60 + struct[11];
  54. if ( struct[9] === '+' ) {
  55. minutesOffset = 0 - minutesOffset;
  56. }
  57. }
  58. timestamp = Date.UTC( struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7] );
  59. }
  60. else {
  61. timestamp = origParse ? origParse( date ) : NaN;
  62. }
  63. return timestamp;
  64. };
  65. }
  66. window.wp = window.wp || {};
  67. wp.api = wp.api || {};
  68. wp.api.utils = wp.api.utils || new WP_API_Utils();
  69. })( Backbone, _, window );
  70. /* global WP_API_Settings:false */
  71. // Suppress warning about parse function's unused "options" argument:
  72. /* jshint unused:false */
  73. (function( wp, WP_API_Settings, Backbone, _, window, undefined ) {
  74. 'use strict';
  75. /**
  76. * Array of parseable dates
  77. *
  78. * @type {string[]}
  79. */
  80. var parseable_dates = [ 'date', 'modified', 'date_gmt', 'modified_gmt' ];
  81. /**
  82. * Mixin for all content that is time stamped
  83. *
  84. * @type {{toJSON: toJSON, parse: parse}}
  85. */
  86. var TimeStampedMixin = {
  87. /**
  88. * Serialize the entity pre-sync
  89. *
  90. * @returns {*}
  91. */
  92. toJSON: function() {
  93. var attributes = _.clone( this.attributes );
  94. // Serialize Date objects back into 8601 strings
  95. _.each( parseable_dates, function ( key ) {
  96. if ( key in attributes ) {
  97. attributes[key] = attributes[key].toISOString();
  98. }
  99. });
  100. return attributes;
  101. },
  102. /**
  103. * Unserialize the fetched response
  104. *
  105. * @param {*} response
  106. * @returns {*}
  107. */
  108. parse: function( response ) {
  109. // Parse dates into native Date objects
  110. _.each( parseable_dates, function ( key ) {
  111. if ( ! ( key in response ) ) {
  112. return;
  113. }
  114. var timestamp = wp.api.utils.parseISO8601( response[key] );
  115. response[key] = new Date( timestamp );
  116. });
  117. // Parse the author into a User object
  118. if ( response.author !== 'undefined' ) {
  119. response.author = new wp.api.models.User( response.author );
  120. }
  121. return response;
  122. }
  123. };
  124. /**
  125. * Mixin for all hierarchical content types such as posts
  126. *
  127. * @type {{parent: parent}}
  128. */
  129. var HierarchicalMixin = {
  130. /**
  131. * Get parent object
  132. *
  133. * @returns {Backbone.Model}
  134. */
  135. parent: function() {
  136. var object, parent = this.get( 'parent' );
  137. // Return null if we don't have a parent
  138. if ( parent === 0 ) {
  139. return null;
  140. }
  141. var parentModel = this;
  142. if ( typeof this.parentModel !== 'undefined' ) {
  143. /**
  144. * Probably a better way to do this. Perhaps grab a cached version of the
  145. * instantiated model?
  146. */
  147. parentModel = new this.parentModel();
  148. }
  149. // Can we get this from its collection?
  150. if ( parentModel.collection ) {
  151. return parentModel.collection.get( parent );
  152. } else {
  153. // Otherwise, get the object directly
  154. object = new parentModel.constructor( {
  155. ID: parent
  156. });
  157. // Note that this acts asynchronously
  158. object.fetch();
  159. return object;
  160. }
  161. }
  162. };
  163. /**
  164. * Private Backbone base model for all models
  165. */
  166. var BaseModel = Backbone.Model.extend(
  167. /** @lends BaseModel.prototype */
  168. {
  169. /**
  170. * Set nonce header before every Backbone sync
  171. *
  172. * @param {string} method
  173. * @param {Backbone.Model} model
  174. * @param {{beforeSend}, *} options
  175. * @returns {*}
  176. */
  177. sync: function( method, model, options ) {
  178. options = options || {};
  179. if ( typeof WP_API_Settings.nonce !== 'undefined' ) {
  180. var beforeSend = options.beforeSend;
  181. options.beforeSend = function( xhr ) {
  182. xhr.setRequestHeader( 'X-WP-Nonce', WP_API_Settings.nonce );
  183. if ( beforeSend ) {
  184. return beforeSend.apply( this, arguments );
  185. }
  186. };
  187. }
  188. return Backbone.sync( method, model, options );
  189. }
  190. }
  191. );
  192. /**
  193. * Backbone model for single users
  194. */
  195. wp.api.models.User = BaseModel.extend(
  196. /** @lends User.prototype */
  197. {
  198. idAttribute: 'ID',
  199. urlRoot: WP_API_Settings.root + '/users',
  200. defaults: {
  201. ID: null,
  202. username: '',
  203. email: '',
  204. password: '',
  205. name: '',
  206. first_name: '',
  207. last_name: '',
  208. nickname: '',
  209. slug: '',
  210. URL: '',
  211. avatar: '',
  212. meta: {
  213. links: {}
  214. }
  215. },
  216. /**
  217. * Return avatar URL
  218. *
  219. * @param {number} size
  220. * @returns {string}
  221. */
  222. avatar: function( size ) {
  223. return this.get( 'avatar' ) + '&s=' + size;
  224. }
  225. }
  226. );
  227. /**
  228. * Model for Taxonomy
  229. */
  230. wp.api.models.Taxonomy = BaseModel.extend(
  231. /** @lends Taxonomy.prototype */
  232. {
  233. idAttribute: 'slug',
  234. urlRoot: WP_API_Settings.root + '/taxonomies',
  235. defaults: {
  236. name: '',
  237. slug: null,
  238. labels: {},
  239. types: {},
  240. show_cloud: false,
  241. hierarchical: false,
  242. meta: {
  243. links: {}
  244. }
  245. }
  246. }
  247. );
  248. /**
  249. * Backbone model for term
  250. */
  251. wp.api.models.Term = BaseModel.extend( _.extend(
  252. /** @lends Term.prototype */
  253. {
  254. idAttribute: 'ID',
  255. taxonomy: 'category',
  256. /**
  257. * @class Represent a term
  258. * @augments Backbone.Model
  259. * @constructs
  260. */
  261. initialize: function( attributes, options ) {
  262. if ( typeof options !== 'undefined' ) {
  263. if ( options.taxonomy ) {
  264. this.taxonomy = options.taxonomy;
  265. }
  266. }
  267. },
  268. /**
  269. * Return URL for the model
  270. *
  271. * @returns {string}
  272. */
  273. url: function() {
  274. var id = this.get( 'ID' );
  275. id = id || '';
  276. return WP_API_Settings.root + '/taxonomies/' + this.taxonomy + '/terms/' + id;
  277. },
  278. defaults: {
  279. ID: null,
  280. name: '',
  281. slug: '',
  282. description: '',
  283. parent: null,
  284. count: 0,
  285. link: '',
  286. meta: {
  287. links: {}
  288. }
  289. }
  290. }, TimeStampedMixin, HierarchicalMixin )
  291. );
  292. /**
  293. * Backbone model for single posts
  294. */
  295. wp.api.models.Post = BaseModel.extend( _.extend(
  296. /** @lends Post.prototype */
  297. {
  298. idAttribute: 'ID',
  299. urlRoot: WP_API_Settings.root + '/posts',
  300. defaults: {
  301. ID: null,
  302. title: '',
  303. status: 'draft',
  304. type: 'post',
  305. author: new wp.api.models.User(),
  306. content: '',
  307. link: '',
  308. 'parent': 0,
  309. date: new Date(),
  310. date_gmt: new Date(),
  311. modified: new Date(),
  312. modified_gmt: new Date(),
  313. format: 'standard',
  314. slug: '',
  315. guid: '',
  316. excerpt: '',
  317. menu_order: 0,
  318. comment_status: 'open',
  319. ping_status: 'open',
  320. sticky: false,
  321. date_tz: 'Etc/UTC',
  322. modified_tz: 'Etc/UTC',
  323. featured_image: null,
  324. terms: {},
  325. post_meta: {},
  326. meta: {
  327. links: {}
  328. }
  329. }
  330. }, TimeStampedMixin, HierarchicalMixin )
  331. );
  332. /**
  333. * Backbone model for pages
  334. */
  335. wp.api.models.Page = BaseModel.extend( _.extend(
  336. /** @lends Page.prototype */
  337. {
  338. idAttribute: 'ID',
  339. urlRoot: WP_API_Settings.root + '/pages',
  340. defaults: {
  341. ID: null,
  342. title: '',
  343. status: 'draft',
  344. type: 'page',
  345. author: new wp.api.models.User(),
  346. content: '',
  347. parent: 0,
  348. link: '',
  349. date: new Date(),
  350. modified: new Date(),
  351. date_gmt: new Date(),
  352. modified_gmt: new Date(),
  353. date_tz: 'Etc/UTC',
  354. modified_tz: 'Etc/UTC',
  355. format: 'standard',
  356. slug: '',
  357. guid: '',
  358. excerpt: '',
  359. menu_order: 0,
  360. comment_status: 'closed',
  361. ping_status: 'open',
  362. sticky: false,
  363. password: '',
  364. meta: {
  365. links: {}
  366. },
  367. featured_image: null,
  368. terms: []
  369. }
  370. }, TimeStampedMixin, HierarchicalMixin )
  371. );
  372. /**
  373. * Backbone model for revisions
  374. */
  375. wp.api.models.Revision = wp.api.models.Post.extend(
  376. /** @lends Revision.prototype */
  377. {
  378. /**
  379. * Return URL for model
  380. *
  381. * @returns {string}
  382. */
  383. url: function() {
  384. var parent_id = this.get( 'parent' );
  385. parent_id = parent_id || '';
  386. var id = this.get( 'ID' );
  387. id = id || '';
  388. return WP_API_Settings.root + '/posts/' + parent_id + '/revisions/' + id;
  389. },
  390. /**
  391. * @class Represent a revision
  392. * @augments Backbone.Model
  393. * @constructs
  394. */
  395. initialize: function() {
  396. // Todo: what of the parent model is a page?
  397. this.parentModel = wp.api.models.Post;
  398. }
  399. }
  400. );
  401. /**
  402. * Backbone model for media items
  403. */
  404. wp.api.models.Media = BaseModel.extend( _.extend(
  405. /** @lends Media.prototype */
  406. {
  407. idAttribute: 'ID',
  408. urlRoot: WP_API_Settings.root + '/media',
  409. defaults: {
  410. ID: null,
  411. title: '',
  412. status: 'inherit',
  413. type: 'attachment',
  414. author: new wp.api.models.User(),
  415. content: '',
  416. parent: 0,
  417. link: '',
  418. date: new Date(),
  419. modified: new Date(),
  420. format: 'standard',
  421. slug: '',
  422. guid: '',
  423. excerpt: '',
  424. menu_order: 0,
  425. comment_status: 'open',
  426. ping_status: 'open',
  427. sticky: false,
  428. date_tz: 'Etc/UTC',
  429. modified_tz: 'Etc/UTC',
  430. date_gmt: new Date(),
  431. modified_gmt: new Date(),
  432. meta: {
  433. links: {}
  434. },
  435. terms: [],
  436. source: '',
  437. is_image: true,
  438. attachment_meta: {},
  439. image_meta: {}
  440. },
  441. /**
  442. * @class Represent a media item
  443. * @augments Backbone.Model
  444. * @constructs
  445. */
  446. initialize: function() {
  447. // Todo: what of the parent model is a page?
  448. this.parentModel = wp.api.models.Post;
  449. }
  450. }, TimeStampedMixin, HierarchicalMixin )
  451. );
  452. /**
  453. * Backbone model for comments
  454. */
  455. wp.api.models.Comment = BaseModel.extend( _.extend(
  456. /** @lends Comment.prototype */
  457. {
  458. idAttribute: 'ID',
  459. defaults: {
  460. ID: null,
  461. post: null,
  462. content: '',
  463. status: 'hold',
  464. type: '',
  465. parent: 0,
  466. author: new wp.api.models.User(),
  467. date: new Date(),
  468. date_gmt: new Date(),
  469. date_tz: 'Etc/UTC',
  470. meta: {
  471. links: {}
  472. }
  473. },
  474. /**
  475. * Return URL for model
  476. *
  477. * @returns {string}
  478. */
  479. url: function() {
  480. var post_id = this.get( 'post' );
  481. post_id = post_id || '';
  482. var id = this.get( 'ID' );
  483. id = id || '';
  484. return WP_API_Settings.root + '/posts/' + post_id + '/comments/' + id;
  485. }
  486. }, TimeStampedMixin, HierarchicalMixin )
  487. );
  488. /**
  489. * Backbone model for single post types
  490. */
  491. wp.api.models.PostType = BaseModel.extend(
  492. /** @lends PostType.prototype */
  493. {
  494. idAttribute: 'slug',
  495. urlRoot: WP_API_Settings.root + '/posts/types',
  496. defaults: {
  497. slug: null,
  498. name: '',
  499. description: '',
  500. labels: {},
  501. queryable: false,
  502. searchable: false,
  503. hierarchical: false,
  504. meta: {
  505. links: {}
  506. },
  507. taxonomies: []
  508. },
  509. /**
  510. * Prevent model from being saved
  511. *
  512. * @returns {boolean}
  513. */
  514. save: function () {
  515. return false;
  516. },
  517. /**
  518. * Prevent model from being deleted
  519. *
  520. * @returns {boolean}
  521. */
  522. 'delete': function () {
  523. return false;
  524. }
  525. }
  526. );
  527. /**
  528. * Backbone model for a post status
  529. */
  530. wp.api.models.PostStatus = BaseModel.extend(
  531. /** @lends PostStatus.prototype */
  532. {
  533. idAttribute: 'slug',
  534. urlRoot: WP_API_Settings.root + '/posts/statuses',
  535. defaults: {
  536. slug: null,
  537. name: '',
  538. 'public': true,
  539. 'protected': false,
  540. 'private': false,
  541. queryable: true,
  542. show_in_list: true,
  543. meta: {
  544. links: {}
  545. }
  546. },
  547. /**
  548. * Prevent model from being saved
  549. *
  550. * @returns {boolean}
  551. */
  552. save: function() {
  553. return false;
  554. },
  555. /**
  556. * Prevent model from being deleted
  557. *
  558. * @returns {boolean}
  559. */
  560. 'delete': function() {
  561. return false;
  562. }
  563. }
  564. );
  565. })( wp, WP_API_Settings, Backbone, _, window );
  566. /* global WP_API_Settings:false */
  567. (function( wp, WP_API_Settings, Backbone, _, window, undefined ) {
  568. 'use strict';
  569. var BaseCollection = Backbone.Collection.extend(
  570. /** @lends BaseCollection.prototype */
  571. {
  572. /**
  573. * Setup default state
  574. */
  575. initialize: function() {
  576. this.state = {
  577. data: {},
  578. currentPage: null,
  579. totalPages: null,
  580. totalObjects: null
  581. };
  582. },
  583. /**
  584. * Overwrite Backbone.Collection.sync to pagination state based on response headers.
  585. *
  586. * Set nonce header before every Backbone sync.
  587. *
  588. * @param {string} method
  589. * @param {Backbone.Model} model
  590. * @param {{success}, *} options
  591. * @returns {*}
  592. */
  593. sync: function( method, model, options ) {
  594. options = options || {};
  595. var beforeSend = options.beforeSend;
  596. if ( typeof WP_API_Settings.nonce !== 'undefined' ) {
  597. options.beforeSend = function( xhr ) {
  598. xhr.setRequestHeader( 'X-WP-Nonce', WP_API_Settings.nonce );
  599. if ( beforeSend ) {
  600. return beforeSend.apply( this, arguments );
  601. }
  602. };
  603. }
  604. if ( 'read' === method ) {
  605. var SELF = this;
  606. if ( options.data ) {
  607. SELF.state.data = _.clone( options.data );
  608. delete SELF.state.data.page;
  609. } else {
  610. SELF.state.data = options.data = {};
  611. }
  612. if ( typeof options.data.page === 'undefined' ) {
  613. SELF.state.currentPage = null;
  614. SELF.state.totalPages = null;
  615. SELF.state.totalObjects = null;
  616. } else {
  617. SELF.state.currentPage = options.data.page - 1;
  618. }
  619. var success = options.success;
  620. options.success = function( data, textStatus, request ) {
  621. SELF.state.totalPages = parseInt( request.getResponseHeader( 'X-WP-TotalPages' ), 10 );
  622. SELF.state.totalObjects = parseInt( request.getResponseHeader( 'X-WP-Total' ), 10 );
  623. if ( SELF.state.currentPage === null ) {
  624. SELF.state.currentPage = 1;
  625. } else {
  626. SELF.state.currentPage++;
  627. }
  628. if ( success ) {
  629. return success.apply( this, arguments );
  630. }
  631. };
  632. }
  633. return Backbone.sync( method, model, options );
  634. },
  635. /**
  636. * Fetches the next page of objects if a new page exists
  637. *
  638. * @param {data: {page}} options
  639. * @returns {*}
  640. */
  641. more: function( options ) {
  642. options = options || {};
  643. options.data = options.data || {};
  644. _.extend( options.data, this.state.data );
  645. if ( typeof options.data.page === 'undefined' ) {
  646. if ( ! this.hasMore() ) {
  647. return false;
  648. }
  649. if ( this.state.currentPage === null || this.state.currentPage <= 1 ) {
  650. options.data.page = 2;
  651. } else {
  652. options.data.page = this.state.currentPage + 1;
  653. }
  654. }
  655. return this.fetch( options );
  656. },
  657. /**
  658. * Returns true if there are more pages of objects available
  659. *
  660. * @returns null|boolean
  661. */
  662. hasMore: function() {
  663. if ( this.state.totalPages === null ||
  664. this.state.totalObjects === null ||
  665. this.state.currentPage === null ) {
  666. return null;
  667. } else {
  668. return ( this.state.currentPage < this.state.totalPages );
  669. }
  670. }
  671. }
  672. );
  673. /**
  674. * Backbone collection for posts
  675. */
  676. wp.api.collections.Posts = BaseCollection.extend(
  677. /** @lends Posts.prototype */
  678. {
  679. url: WP_API_Settings.root + '/posts',
  680. model: wp.api.models.Post
  681. }
  682. );
  683. /**
  684. * Backbone collection for pages
  685. */
  686. wp.api.collections.Pages = BaseCollection.extend(
  687. /** @lends Pages.prototype */
  688. {
  689. url: WP_API_Settings.root + '/pages',
  690. model: wp.api.models.Page
  691. }
  692. );
  693. /**
  694. * Backbone users collection
  695. */
  696. wp.api.collections.Users = BaseCollection.extend(
  697. /** @lends Users.prototype */
  698. {
  699. url: WP_API_Settings.root + '/users',
  700. model: wp.api.models.User
  701. }
  702. );
  703. /**
  704. * Backbone post statuses collection
  705. */
  706. wp.api.collections.PostStatuses = BaseCollection.extend(
  707. /** @lends PostStatuses.prototype */
  708. {
  709. url: WP_API_Settings.root + '/posts/statuses',
  710. model: wp.api.models.PostStatus
  711. }
  712. );
  713. /**
  714. * Backbone media library collection
  715. */
  716. wp.api.collections.MediaLibrary = BaseCollection.extend(
  717. /** @lends MediaLibrary.prototype */
  718. {
  719. url: WP_API_Settings.root + '/media',
  720. model: wp.api.models.Media
  721. }
  722. );
  723. /**
  724. * Backbone taxonomy collection
  725. */
  726. wp.api.collections.Taxonomies = BaseCollection.extend(
  727. /** @lends Taxonomies.prototype */
  728. {
  729. model: wp.api.models.Taxonomy,
  730. url: WP_API_Settings.root + '/taxonomies'
  731. }
  732. );
  733. /**
  734. * Backbone comment collection
  735. */
  736. wp.api.collections.Comments = BaseCollection.extend(
  737. /** @lends Comments.prototype */
  738. {
  739. model: wp.api.models.Comment,
  740. post: null,
  741. /**
  742. * @class Represent an array of comments
  743. * @augments Backbone.Collection
  744. * @constructs
  745. */
  746. initialize: function( models, options ) {
  747. this.constructor.__super__.initialize.apply( this, arguments );
  748. if ( options && options.post ) {
  749. this.post = options.post;
  750. }
  751. },
  752. /**
  753. * Return URL for collection
  754. *
  755. * @returns {string}
  756. */
  757. url: function() {
  758. return WP_API_Settings.root + '/posts/' + this.post + '/comments';
  759. }
  760. }
  761. );
  762. /**
  763. * Backbone post type collection
  764. */
  765. wp.api.collections.PostTypes = BaseCollection.extend(
  766. /** @lends PostTypes.prototype */
  767. {
  768. model: wp.api.models.PostType,
  769. url: WP_API_Settings.root + '/posts/types'
  770. }
  771. );
  772. /**
  773. * Backbone terms collection
  774. */
  775. wp.api.collections.Terms = BaseCollection.extend(
  776. /** @lends Terms.prototype */
  777. {
  778. model: wp.api.models.Term,
  779. type: 'post',
  780. taxonomy: 'category',
  781. /**
  782. * @class Represent an array of terms
  783. * @augments Backbone.Collection
  784. * @constructs
  785. */
  786. initialize: function( models, options ) {
  787. this.constructor.__super__.initialize.apply( this, arguments );
  788. if ( typeof options !== 'undefined' ) {
  789. if ( options.type ) {
  790. this.type = options.type;
  791. }
  792. if ( options.taxonomy ) {
  793. this.taxonomy = options.taxonomy;
  794. }
  795. }
  796. this.on( 'add', _.bind( this.addModel, this ) );
  797. },
  798. /**
  799. * We need to set the type and taxonomy for each model
  800. *
  801. * @param {Backbone.model} model
  802. */
  803. addModel: function( model ) {
  804. model.type = this.type;
  805. model.taxonomy = this.taxonomy;
  806. },
  807. /**
  808. * Return URL for collection
  809. *
  810. * @returns {string}
  811. */
  812. url: function() {
  813. return WP_API_Settings.root + '/posts/types/' + this.type + '/taxonomies/' + this.taxonomy + '/terms/';
  814. }
  815. }
  816. );
  817. /**
  818. * Backbone revisions collection
  819. */
  820. wp.api.collections.Revisions = BaseCollection.extend(
  821. /** @lends Revisions.prototype */
  822. {
  823. model: wp.api.models.Revision,
  824. parent: null,
  825. /**
  826. * @class Represent an array of revisions
  827. * @augments Backbone.Collection
  828. * @constructs
  829. */
  830. initialize: function( models, options ) {
  831. this.constructor.__super__.initialize.apply( this, arguments );
  832. if ( options && options.parent ) {
  833. this.parent = options.parent;
  834. }
  835. },
  836. /**
  837. * return URL for collection
  838. *
  839. * @returns {string}
  840. */
  841. url: function() {
  842. return WP_API_Settings.root + '/posts/' + this.parent + '/revisions';
  843. }
  844. }
  845. );
  846. })( wp, WP_API_Settings, Backbone, _, window );