This repository was archived by the owner on Jun 26, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -274,6 +274,22 @@ export default class View {
274274 children . map ( c => this . _unboundChildren . add ( c ) ) ;
275275 }
276276
277+ /**
278+ * The opposite of {@link #addChildren}. Removes a child view from this view instance.
279+ * Once removed, the child is no longer managed by its parent, e.g. it can be safely used elsewhere,
280+ * becoming a child of another parent view.
281+ *
282+ * @see #addChildren
283+ * @param {module:ui/view~View|Iterable.<module:ui/view~View> } children Child views to be removed.
284+ */
285+ removeChildren ( children ) {
286+ if ( ! isIterable ( children ) ) {
287+ children = [ children ] ;
288+ }
289+
290+ children . map ( c => this . _unboundChildren . remove ( c ) ) ;
291+ }
292+
277293 /**
278294 * Initializes the view and child views located in {@link #_viewCollections}.
279295 */
Original file line number Diff line number Diff line change @@ -105,6 +105,39 @@ describe( 'View', () => {
105105 } ) ;
106106 } ) ;
107107
108+ describe ( 'removeChildren()' , ( ) => {
109+ beforeEach ( ( ) => {
110+ setTestViewClass ( ) ;
111+ setTestViewInstance ( ) ;
112+ } ) ;
113+
114+ it ( 'should remove a single view from #_unboundChildren' , ( ) => {
115+ const child1 = { } ;
116+ const child2 = { } ;
117+
118+ view . addChildren ( child1 ) ;
119+ view . addChildren ( child2 ) ;
120+ expect ( view . _unboundChildren ) . to . have . length ( 2 ) ;
121+
122+ view . removeChildren ( child2 ) ;
123+ expect ( view . _unboundChildren ) . to . have . length ( 1 ) ;
124+ expect ( view . _unboundChildren . get ( 0 ) ) . to . equal ( child1 ) ;
125+ } ) ;
126+
127+ it ( 'should support iterables' , ( ) => {
128+ const child1 = { } ;
129+ const child2 = { } ;
130+ const child3 = { } ;
131+
132+ view . addChildren ( [ child1 , child2 , child3 ] ) ;
133+ expect ( view . _unboundChildren ) . to . have . length ( 3 ) ;
134+
135+ view . removeChildren ( [ child2 , child3 ] ) ;
136+ expect ( view . _unboundChildren ) . to . have . length ( 1 ) ;
137+ expect ( view . _unboundChildren . get ( 0 ) ) . to . equal ( child1 ) ;
138+ } ) ;
139+ } ) ;
140+
108141 describe ( 'init()' , ( ) => {
109142 beforeEach ( createViewWithChildren ) ;
110143
You can’t perform that action at this time.
0 commit comments