_apply()

in src/Bootstrapper.js [62:119]


	_apply(nodes, cb = null) {
		// const factory = container.domFactory();
		// const id = container.id();
		const resolveQueue = [];

		// Iterate over component elements in the dom
		for (let i = 0; i < nodes.length; ++i) {
			const ele = nodes[i];

			// Ignore elements that have already been connected
			if (Habitat.hasHabitat(ele)) {
				continue;
			}

			// Resolve components using promises
			const componentName = ele.getAttribute(this.componentSelector);
			resolveQueue.push(
				this.__container__
					.resolve(componentName, this)
					.then((registration) => {
						// This is an expensive operation so only do on non prod builds
						if (process.env.NODE_ENV !== 'production') {
							if (ele.querySelector(`[${this.componentSelector}]`)) {
								Logger.warn('RHW08', 'Component should not contain any nested components.', ele);
							}
						}

						// Generate props
						let props = Habitat.parseProps(ele);
						if (registration.meta.defaultProps) {
							props = Object.assign({}, registration.meta.defaultProps, props);
						}

						// Options
						const options = registration.meta.options || {};

						// Inject the component
						this.__container__.factory.inject(
							registration.component,
							props,
							Habitat.create(ele, this.__container__.id, options));
					}).catch((err) => {
						Logger.error('RHW01', `Cannot resolve component "${componentName}" for element.`, err, ele);
					}),
			);
		}

		// Trigger callback when all promises are finished
		// regardless if some fail
		Promise
			.all(resolveQueue.map(p => p.catch(e => e)))
			.then(() => {
				_callback(cb);
			}).catch((err) => {
			// We should never get here.. if we do this is a bug
				throw err;
			});
	}