From 0378527d526851458bdd64555057039ab59f8043 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sun, 21 Apr 2019 15:45:49 +0200 Subject: [PATCH] Add `getTraitIndex` method to Component --- src/dom_components/model/Component.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index f232ac6fc..b0a733c7e 100644 --- a/src/dom_components/model/Component.js +++ b/src/dom_components/model/Component.js @@ -677,6 +677,20 @@ const Component = Backbone.Model.extend(Styleable).extend( })[0]; }, + /** + * Get the trait position index by id/name. Useful in case you want to + * replace some trait, at runtime, with something else. + * @param {String} id The `id` or `name` of the trait + * @return {Number} Index position of the current trait + * @example + * const traitTitle = component.getTraitIndex('title'); + * console.log(traitTitle); // 1 + */ + getTraitIndex(id) { + const trait = this.getTrait(id); + return trait ? this.get('traits').indexOf(trait) : trait; + }, + /** * Normalize input classes from array to array of objects * @param {Array} arr