collection.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module utils/collection
  7. */
  8. import EmitterMixin from './emittermixin';
  9. import CKEditorError from './ckeditorerror';
  10. import uid from './uid';
  11. import isIterable from './isiterable';
  12. import mix from './mix';
  13. /**
  14. * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
  15. * in the collection (like in an array) or by their ids.
  16. *
  17. * If an object without an `id` property is being added to the collection, the `id` property will be generated
  18. * automatically. Note that the automatically generated id is unique only within this single collection instance.
  19. *
  20. * By default an item in the collection is identified by its `id` property. The name of the identifier can be
  21. * configured through the constructor of the collection.
  22. *
  23. * @mixes module:utils/emittermixin~EmitterMixin
  24. */
  25. export default class Collection {
  26. /**
  27. * Creates a new Collection instance.
  28. *
  29. * You can provide an iterable of initial items the collection will be created with:
  30. *
  31. * const collection = new Collection( [ { id: 'John' }, { id: 'Mike' } ] );
  32. *
  33. * console.log( collection.get( 0 ) ); // -> { id: 'John' }
  34. * console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
  35. * console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
  36. *
  37. * Or you can first create a collection and then add new items using the {@link #add} method:
  38. *
  39. * const collection = new Collection();
  40. *
  41. * collection.add( { id: 'John' } );
  42. * console.log( collection.get( 0 ) ); // -> { id: 'John' }
  43. *
  44. * Whatever option you choose, you can always pass a configuration object as the last argument
  45. * of the constructor:
  46. *
  47. * const emptyCollection = new Collection( { idProperty: 'name' } );
  48. * emptyCollection.add( { name: 'John' } );
  49. * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
  50. *
  51. * const nonEmptyCollection = new Collection( [ { name: 'John' } ], { idProperty: 'name' } );
  52. * nonEmptyCollection.add( { name: 'George' } );
  53. * console.log( collection.get( 'George' ) ); // -> { name: 'George' }
  54. * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
  55. *
  56. * @param {Iterable.<Object>|Object} initialItemsOrOptions The initial items of the collection or
  57. * the options object.
  58. * @param {Object} [options={}] The options object, when the first argument is an array of initial items.
  59. * @param {String} [options.idProperty='id'] The name of the property which is used to identify an item.
  60. * Items that do not have such a property will be assigned one when added to the collection.
  61. */
  62. constructor( initialItemsOrOptions = {}, options = {} ) {
  63. const hasInitialItems = isIterable( initialItemsOrOptions );
  64. if ( !hasInitialItems ) {
  65. options = initialItemsOrOptions;
  66. }
  67. /**
  68. * The internal list of items in the collection.
  69. *
  70. * @private
  71. * @member {Object[]}
  72. */
  73. this._items = [];
  74. /**
  75. * The internal map of items in the collection.
  76. *
  77. * @private
  78. * @member {Map}
  79. */
  80. this._itemMap = new Map();
  81. /**
  82. * The name of the property which is considered to identify an item.
  83. *
  84. * @private
  85. * @member {String}
  86. */
  87. this._idProperty = options.idProperty || 'id';
  88. /**
  89. * A helper mapping external items of a bound collection ({@link #bindTo})
  90. * and actual items of this collection. It provides information
  91. * necessary to properly remove items bound to another collection.
  92. *
  93. * See {@link #_bindToInternalToExternalMap}.
  94. *
  95. * @protected
  96. * @member {WeakMap}
  97. */
  98. this._bindToExternalToInternalMap = new WeakMap();
  99. /**
  100. * A helper mapping items of this collection to external items of a bound collection
  101. * ({@link #bindTo}). It provides information necessary to manage the bindings, e.g.
  102. * to avoid loops in two–way bindings.
  103. *
  104. * See {@link #_bindToExternalToInternalMap}.
  105. *
  106. * @protected
  107. * @member {WeakMap}
  108. */
  109. this._bindToInternalToExternalMap = new WeakMap();
  110. /**
  111. * Stores indexes of skipped items from bound external collection.
  112. *
  113. * @private
  114. * @member {Array}
  115. */
  116. this._skippedIndexesFromExternal = [];
  117. // Set the initial content of the collection (if provided in the constructor).
  118. if ( hasInitialItems ) {
  119. for ( const item of initialItemsOrOptions ) {
  120. this._items.push( item );
  121. this._itemMap.set( this._getItemIdBeforeAdding( item ), item );
  122. }
  123. }
  124. /**
  125. * A collection instance this collection is bound to as a result
  126. * of calling {@link #bindTo} method.
  127. *
  128. * @protected
  129. * @member {module:utils/collection~Collection} #_bindToCollection
  130. */
  131. }
  132. /**
  133. * The number of items available in the collection.
  134. *
  135. * @member {Number} #length
  136. */
  137. get length() {
  138. return this._items.length;
  139. }
  140. /**
  141. * Returns the first item from the collection or null when collection is empty.
  142. *
  143. * @returns {Object|null} The first item or `null` if collection is empty.
  144. */
  145. get first() {
  146. return this._items[ 0 ] || null;
  147. }
  148. /**
  149. * Returns the last item from the collection or null when collection is empty.
  150. *
  151. * @returns {Object|null} The last item or `null` if collection is empty.
  152. */
  153. get last() {
  154. return this._items[ this.length - 1 ] || null;
  155. }
  156. /**
  157. * Adds an item into the collection.
  158. *
  159. * If the item does not have an id, then it will be automatically generated and set on the item.
  160. *
  161. * @chainable
  162. * @param {Object} item
  163. * @param {Number} [index] The position of the item in the collection. The item
  164. * is pushed to the collection when `index` not specified.
  165. * @fires add
  166. * @fires addBatch
  167. */
  168. add( item, index ) {
  169. return this.addBatch( [ item ], index );
  170. }
  171. /**
  172. * Adds multiple items into the collection.
  173. *
  174. * Any item not containing an id will get an automatically generated one.
  175. *
  176. * @chainable
  177. * @param {Iterable.<Object>} item
  178. * @param {Number} [index] The position of the insertion. Items will be appended if no `index` is specified.
  179. * @fires add
  180. * @fires addBatch
  181. */
  182. addBatch( items, index ) {
  183. if ( index === undefined ) {
  184. index = this._items.length;
  185. }
  186. if ( index > this._items.length || index < 0 ) {
  187. /**
  188. * The index number has invalid value.
  189. *
  190. * @error collection-add-item-bad-index
  191. */
  192. throw new CKEditorError( 'collection-add-item-invalid-index', this );
  193. }
  194. for ( let offset = 0; offset < items.length; offset++ ) {
  195. const item = items[ offset ];
  196. const itemId = this._getItemIdBeforeAdding( item );
  197. const currentItemIndex = index + offset;
  198. this._items.splice( currentItemIndex, 0, item );
  199. this._itemMap.set( itemId, item );
  200. this.fire( 'add', item, currentItemIndex );
  201. }
  202. this.fire( 'addBatch', items, index );
  203. return this;
  204. }
  205. /**
  206. * Gets item by its id or index.
  207. *
  208. * @param {String|Number} idOrIndex The item id or index in the collection.
  209. * @returns {Object|null} The requested item or `null` if such item does not exist.
  210. */
  211. get( idOrIndex ) {
  212. let item;
  213. if ( typeof idOrIndex == 'string' ) {
  214. item = this._itemMap.get( idOrIndex );
  215. } else if ( typeof idOrIndex == 'number' ) {
  216. item = this._items[ idOrIndex ];
  217. } else {
  218. /**
  219. * Index or id must be given.
  220. *
  221. * @error collection-get-invalid-arg
  222. */
  223. throw new CKEditorError( 'collection-get-invalid-arg: Index or id must be given.', this );
  224. }
  225. return item || null;
  226. }
  227. /**
  228. * Returns a boolean indicating whether the collection contains an item.
  229. *
  230. * @param {Object|String} itemOrId The item or its id in the collection.
  231. * @returns {Boolean} `true` if the collection contains the item, `false` otherwise.
  232. */
  233. has( itemOrId ) {
  234. if ( typeof itemOrId == 'string' ) {
  235. return this._itemMap.has( itemOrId );
  236. } else { // Object
  237. const idProperty = this._idProperty;
  238. const id = itemOrId[ idProperty ];
  239. return this._itemMap.has( id );
  240. }
  241. }
  242. /**
  243. * Gets index of item in the collection.
  244. * When item is not defined in the collection then index will be equal -1.
  245. *
  246. * @param {Object|String} itemOrId The item or its id in the collection.
  247. * @returns {Number} Index of given item.
  248. */
  249. getIndex( itemOrId ) {
  250. let item;
  251. if ( typeof itemOrId == 'string' ) {
  252. item = this._itemMap.get( itemOrId );
  253. } else {
  254. item = itemOrId;
  255. }
  256. return this._items.indexOf( item );
  257. }
  258. /**
  259. * Removes an item from the collection.
  260. *
  261. * @param {Object|Number|String} subject The item to remove, its id or index in the collection.
  262. * @returns {Object} The removed item.
  263. * @fires remove
  264. */
  265. remove( subject ) {
  266. let index, id, item;
  267. let itemDoesNotExist = false;
  268. const idProperty = this._idProperty;
  269. if ( typeof subject == 'string' ) {
  270. id = subject;
  271. item = this._itemMap.get( id );
  272. itemDoesNotExist = !item;
  273. if ( item ) {
  274. index = this._items.indexOf( item );
  275. }
  276. } else if ( typeof subject == 'number' ) {
  277. index = subject;
  278. item = this._items[ index ];
  279. itemDoesNotExist = !item;
  280. if ( item ) {
  281. id = item[ idProperty ];
  282. }
  283. } else {
  284. item = subject;
  285. id = item[ idProperty ];
  286. index = this._items.indexOf( item );
  287. itemDoesNotExist = ( index == -1 || !this._itemMap.get( id ) );
  288. }
  289. if ( itemDoesNotExist ) {
  290. /**
  291. * Item not found.
  292. *
  293. * @error collection-remove-404
  294. */
  295. throw new CKEditorError( 'collection-remove-404: Item not found.', this );
  296. }
  297. this._items.splice( index, 1 );
  298. this._itemMap.delete( id );
  299. const externalItem = this._bindToInternalToExternalMap.get( item );
  300. this._bindToInternalToExternalMap.delete( item );
  301. this._bindToExternalToInternalMap.delete( externalItem );
  302. this.fire( 'remove', item, index );
  303. return item;
  304. }
  305. /**
  306. * Executes the callback for each item in the collection and composes an array or values returned by this callback.
  307. *
  308. * @param {Function} callback
  309. * @param {Object} callback.item
  310. * @param {Number} callback.index
  311. * @param {Object} ctx Context in which the `callback` will be called.
  312. * @returns {Array} The result of mapping.
  313. */
  314. map( callback, ctx ) {
  315. return this._items.map( callback, ctx );
  316. }
  317. /**
  318. * Finds the first item in the collection for which the `callback` returns a true value.
  319. *
  320. * @param {Function} callback
  321. * @param {Object} callback.item
  322. * @param {Number} callback.index
  323. * @param {Object} ctx Context in which the `callback` will be called.
  324. * @returns {Object} The item for which `callback` returned a true value.
  325. */
  326. find( callback, ctx ) {
  327. return this._items.find( callback, ctx );
  328. }
  329. /**
  330. * Returns an array with items for which the `callback` returned a true value.
  331. *
  332. * @param {Function} callback
  333. * @param {Object} callback.item
  334. * @param {Number} callback.index
  335. * @param {Object} ctx Context in which the `callback` will be called.
  336. * @returns {Object[]} The array with matching items.
  337. */
  338. filter( callback, ctx ) {
  339. return this._items.filter( callback, ctx );
  340. }
  341. /**
  342. * Removes all items from the collection and destroys the binding created using
  343. * {@link #bindTo}.
  344. */
  345. clear() {
  346. if ( this._bindToCollection ) {
  347. this.stopListening( this._bindToCollection );
  348. this._bindToCollection = null;
  349. }
  350. while ( this.length ) {
  351. this.remove( 0 );
  352. }
  353. }
  354. /**
  355. * Binds and synchronizes the collection with another one.
  356. *
  357. * The binding can be a simple factory:
  358. *
  359. * class FactoryClass {
  360. * constructor( data ) {
  361. * this.label = data.label;
  362. * }
  363. * }
  364. *
  365. * const source = new Collection( { idProperty: 'label' } );
  366. * const target = new Collection();
  367. *
  368. * target.bindTo( source ).as( FactoryClass );
  369. *
  370. * source.add( { label: 'foo' } );
  371. * source.add( { label: 'bar' } );
  372. *
  373. * console.log( target.length ); // 2
  374. * console.log( target.get( 1 ).label ); // 'bar'
  375. *
  376. * source.remove( 0 );
  377. * console.log( target.length ); // 1
  378. * console.log( target.get( 0 ).label ); // 'bar'
  379. *
  380. * or the factory driven by a custom callback:
  381. *
  382. * class FooClass {
  383. * constructor( data ) {
  384. * this.label = data.label;
  385. * }
  386. * }
  387. *
  388. * class BarClass {
  389. * constructor( data ) {
  390. * this.label = data.label;
  391. * }
  392. * }
  393. *
  394. * const source = new Collection( { idProperty: 'label' } );
  395. * const target = new Collection();
  396. *
  397. * target.bindTo( source ).using( ( item ) => {
  398. * if ( item.label == 'foo' ) {
  399. * return new FooClass( item );
  400. * } else {
  401. * return new BarClass( item );
  402. * }
  403. * } );
  404. *
  405. * source.add( { label: 'foo' } );
  406. * source.add( { label: 'bar' } );
  407. *
  408. * console.log( target.length ); // 2
  409. * console.log( target.get( 0 ) instanceof FooClass ); // true
  410. * console.log( target.get( 1 ) instanceof BarClass ); // true
  411. *
  412. * or the factory out of property name:
  413. *
  414. * const source = new Collection( { idProperty: 'label' } );
  415. * const target = new Collection();
  416. *
  417. * target.bindTo( source ).using( 'label' );
  418. *
  419. * source.add( { label: { value: 'foo' } } );
  420. * source.add( { label: { value: 'bar' } } );
  421. *
  422. * console.log( target.length ); // 2
  423. * console.log( target.get( 0 ).value ); // 'foo'
  424. * console.log( target.get( 1 ).value ); // 'bar'
  425. *
  426. * It's possible to skip specified items by returning falsy value:
  427. *
  428. * const source = new Collection();
  429. * const target = new Collection();
  430. *
  431. * target.bindTo( source ).using( item => {
  432. * if ( item.hidden ) {
  433. * return null;
  434. * }
  435. *
  436. * return item;
  437. * } );
  438. *
  439. * source.add( { hidden: true } );
  440. * source.add( { hidden: false } );
  441. *
  442. * console.log( source.length ); // 2
  443. * console.log( target.length ); // 1
  444. *
  445. * **Note**: {@link #clear} can be used to break the binding.
  446. *
  447. * @param {module:utils/collection~Collection} externalCollection A collection to be bound.
  448. * @returns {Object}
  449. * @returns {module:utils/collection~CollectionBindToChain} The binding chain object.
  450. */
  451. bindTo( externalCollection ) {
  452. if ( this._bindToCollection ) {
  453. /**
  454. * The collection cannot be bound more than once.
  455. *
  456. * @error collection-bind-to-rebind
  457. */
  458. throw new CKEditorError( 'collection-bind-to-rebind: The collection cannot be bound more than once.', this );
  459. }
  460. this._bindToCollection = externalCollection;
  461. return {
  462. as: Class => {
  463. this._setUpBindToBinding( item => new Class( item ) );
  464. },
  465. using: callbackOrProperty => {
  466. if ( typeof callbackOrProperty == 'function' ) {
  467. this._setUpBindToBinding( item => callbackOrProperty( item ) );
  468. } else {
  469. this._setUpBindToBinding( item => item[ callbackOrProperty ] );
  470. }
  471. }
  472. };
  473. }
  474. /**
  475. * Finalizes and activates a binding initiated by {#bindTo}.
  476. *
  477. * @protected
  478. * @param {Function} factory A function which produces collection items.
  479. */
  480. _setUpBindToBinding( factory ) {
  481. const externalCollection = this._bindToCollection;
  482. // Adds the item to the collection once a change has been done to the external collection.
  483. //
  484. // @private
  485. const addItem = ( evt, externalItem, index ) => {
  486. const isExternalBoundToThis = externalCollection._bindToCollection == this;
  487. const externalItemBound = externalCollection._bindToInternalToExternalMap.get( externalItem );
  488. // If an external collection is bound to this collection, which makes it a 2–way binding,
  489. // and the particular external collection item is already bound, don't add it here.
  490. // The external item has been created **out of this collection's item** and (re)adding it will
  491. // cause a loop.
  492. if ( isExternalBoundToThis && externalItemBound ) {
  493. this._bindToExternalToInternalMap.set( externalItem, externalItemBound );
  494. this._bindToInternalToExternalMap.set( externalItemBound, externalItem );
  495. } else {
  496. const item = factory( externalItem );
  497. // When there is no item we need to remember skipped index first and then we can skip this item.
  498. if ( !item ) {
  499. this._skippedIndexesFromExternal.push( index );
  500. return;
  501. }
  502. // Lets try to put item at the same index as index in external collection
  503. // but when there are a skipped items in one or both collections we need to recalculate this index.
  504. let finalIndex = index;
  505. // When we try to insert item after some skipped items from external collection we need
  506. // to include this skipped items and decrease index.
  507. //
  508. // For the following example:
  509. // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal' ]
  510. // internal -> [ A ]
  511. //
  512. // Another item is been added at the end of external collection:
  513. // external.add( 'D' )
  514. // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal', 'D' ]
  515. //
  516. // We can't just add 'D' to internal at the same index as index in external because
  517. // this will produce empty indexes what is invalid:
  518. // internal -> [ 'A', empty, empty, 'D' ]
  519. //
  520. // So we need to include skipped items and decrease index
  521. // internal -> [ 'A', 'D' ]
  522. for ( const skipped of this._skippedIndexesFromExternal ) {
  523. if ( index > skipped ) {
  524. finalIndex--;
  525. }
  526. }
  527. // We need to take into consideration that external collection could skip some items from
  528. // internal collection.
  529. //
  530. // For the following example:
  531. // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external' ]
  532. // external -> [ A ]
  533. //
  534. // Another item is been added at the end of external collection:
  535. // external.add( 'D' )
  536. // external -> [ 'A', 'D' ]
  537. //
  538. // We need to include skipped items and place new item after them:
  539. // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external', 'D' ]
  540. for ( const skipped of externalCollection._skippedIndexesFromExternal ) {
  541. if ( finalIndex >= skipped ) {
  542. finalIndex++;
  543. }
  544. }
  545. this._bindToExternalToInternalMap.set( externalItem, item );
  546. this._bindToInternalToExternalMap.set( item, externalItem );
  547. this.add( item, finalIndex );
  548. // After adding new element to internal collection we need update indexes
  549. // of skipped items in external collection.
  550. for ( let i = 0; i < externalCollection._skippedIndexesFromExternal.length; i++ ) {
  551. if ( finalIndex <= externalCollection._skippedIndexesFromExternal[ i ] ) {
  552. externalCollection._skippedIndexesFromExternal[ i ]++;
  553. }
  554. }
  555. }
  556. };
  557. // Load the initial content of the collection.
  558. for ( const externalItem of externalCollection ) {
  559. addItem( null, externalItem, externalCollection.getIndex( externalItem ) );
  560. }
  561. // Synchronize the with collection as new items are added.
  562. this.listenTo( externalCollection, 'add', addItem );
  563. // Synchronize the with collection as new items are removed.
  564. this.listenTo( externalCollection, 'remove', ( evt, externalItem, index ) => {
  565. const item = this._bindToExternalToInternalMap.get( externalItem );
  566. if ( item ) {
  567. this.remove( item );
  568. }
  569. // After removing element from external collection we need update/remove indexes
  570. // of skipped items in internal collection.
  571. this._skippedIndexesFromExternal = this._skippedIndexesFromExternal.reduce( ( result, skipped ) => {
  572. if ( index < skipped ) {
  573. result.push( skipped - 1 );
  574. }
  575. if ( index > skipped ) {
  576. result.push( skipped );
  577. }
  578. return result;
  579. }, [] );
  580. } );
  581. }
  582. /**
  583. * Returns an unique id property for a given `item`.
  584. *
  585. * The method will generate new id and assign it to the `item` if it doesn't have any.
  586. *
  587. * @private
  588. * @param {Object} item Item to be added.
  589. * @returns {String}
  590. */
  591. _getItemIdBeforeAdding( item ) {
  592. const idProperty = this._idProperty;
  593. let itemId;
  594. if ( ( idProperty in item ) ) {
  595. itemId = item[ idProperty ];
  596. if ( typeof itemId != 'string' ) {
  597. /**
  598. * This item's id should be a string.
  599. *
  600. * @error collection-add-invalid-id
  601. */
  602. throw new CKEditorError( 'collection-add-invalid-id', this );
  603. }
  604. if ( this.get( itemId ) ) {
  605. /**
  606. * This item already exists in the collection.
  607. *
  608. * @error collection-add-item-already-exists
  609. */
  610. throw new CKEditorError( 'collection-add-item-already-exists', this );
  611. }
  612. } else {
  613. item[ idProperty ] = itemId = uid();
  614. }
  615. return itemId;
  616. }
  617. /**
  618. * Iterable interface.
  619. *
  620. * @returns {Iterable.<*>}
  621. */
  622. [ Symbol.iterator ]() {
  623. return this._items[ Symbol.iterator ]();
  624. }
  625. /**
  626. * Fired when an item is added to the collection.
  627. *
  628. * @event add
  629. * @param {Object} item The added item.
  630. */
  631. /**
  632. * Fired when an item is removed from the collection.
  633. *
  634. * @event remove
  635. * @param {Object} item The removed item.
  636. * @param {Number} index Index from which item was removed.
  637. */
  638. }
  639. mix( Collection, EmitterMixin );
  640. /**
  641. * An object returned by the {@link module:utils/collection~Collection#bindTo `bindTo()`} method
  642. * providing functions that specify the type of the binding.
  643. *
  644. * See the {@link module:utils/collection~Collection#bindTo `bindTo()`} documentation for examples.
  645. *
  646. * @interface module:utils/collection~CollectionBindToChain
  647. */
  648. /**
  649. * Creates a callback or a property binding.
  650. *
  651. * @method #using
  652. * @param {Function|String} callbackOrProperty When the function is passed, it should return
  653. * the collection items. When the string is provided, the property value is used to create the bound collection items.
  654. */
  655. /**
  656. * Creates the class factory binding in which items of the source collection are passed to
  657. * the constructor of the specified class.
  658. *
  659. * @method #as
  660. * @param {Function} Class The class constructor used to create instances in the factory.
  661. */