Skip to content

Commit

Permalink
Force init priority between mods
Browse files Browse the repository at this point in the history
  • Loading branch information
belozer committed Jun 23, 2018
1 parent f711cfd commit 18487b0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
67 changes: 29 additions & 38 deletions common.blocks/i-bem-dom/i-bem-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function initEntity(entityName, domElem, params, ignoreLazyInit, callback) {

entityCls._processInit();

if(ignoreLazyInit || params.lazyInit === false || !entityCls._lazyInitCheck(domElem[0]) && !params.lazyInit) {
if(ignoreLazyInit || params.lazyInit === false || !entityCls._checkLazyInit(domElem[0]) && !params.lazyInit) {
ignoreLazyInit && domElem.addClass(BEM_CLASS_NAME); // add css class for preventing memory leaks in further destructing

entity = new entityCls(uniqIdToDomElems[uniqId], params, !!ignoreLazyInit);
Expand Down Expand Up @@ -342,7 +342,7 @@ function getEntityBase(baseCls, entityName, base) {
}

/**
* Extract lazyInit property from staticProps
* Extract lazyInit property from static props
* @param {Object} [staticProps]
* @returns {?Boolean}
*/
Expand All @@ -359,20 +359,16 @@ function extractLazyInitProp(staticProps) {
/**
* Processing lazyInit rules for entity
* @param {Function} entity BemDomEntity
* @param {Object} [mod] mod declaration
* @param {Object} mod mod declaration
* @param {Boolean} lazyInit lazyInit behavior
* @returns {?Boolean}
*/
function processLazyInitRule(entity, mod, lazyInit) {
if(arguments.length < 3) {
lazyInit = mod;
mod = undef;
}

var rules = entity._lazyInitRules || (entity._lazyInitRules = []);
var rules = entity._lazyInitRules;

rules.push({
check : mod? entity._buildModValRE(mod.modName, mod.modVal) : entity._buildRE(),
check : entity._buildModValRE(mod.modName, mod.modVal),
modName : mod.modName,
lazyInit : lazyInit
});
}
Expand Down Expand Up @@ -864,15 +860,6 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{
return this.getEntityName() + MOD_DELIM + modName;
},

/**
* Builds a regular expression for check entity on DOM element
* @private
* @returns {RegExp}
*/
_buildRE : function() {
return new RegExp('(\\s|^)' + this.getEntityName() + '?(?=\\s|$)');
},

/**
* Builds a regular expression for extracting modifier values from a DOM element of an entity
* @private
Expand Down Expand Up @@ -914,18 +901,28 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{
/**
* Check domNode for lazy initialization entity
* @protected
* @returns {?Boolean}
* @param {HTMLElement} domNode
* @returns {Boolean|undefined}
*/
_lazyInitCheck : function(domNode) {
var rules = this._lazyInitRules, rule;
if(!rules) return null;
_checkLazyInit : function(domNode) {
var rules = this._lazyInitRules,
len = rules.length,
rule,
lazyInit,
modName;

var len = rules.length;
while(rule = rules[--len]) {
if(rule.check.test(domNode.className)) return rule.lazyInit;
if(modName !== rule.modName && rule.check.test(domNode.className)) {
if(lazyInit === undef || lazyInit === true) {
modName = rule.modName;
lazyInit = rule.lazyInit;
}

if(lazyInit === false) return lazyInit;
}
}

return null;
return lazyInit !== undef? lazyInit : this.lazyInit;
}
});

Expand All @@ -940,6 +937,8 @@ var Block = inherit([bem.Block, BemDomEntity], /** @lends Block.prototype */{
_block : function() {
return this;
}
}, {
_lazyInitRules : []
});

/**
Expand All @@ -953,6 +952,8 @@ var Elem = inherit([bem.Elem, BemDomEntity], /** @lends Elem.prototype */{
_block : function() {
return this._blockInstance || (this._blockInstance = this.findParentBlock(getEntityCls(this.__self._blockName)));
}
}, {
_lazyInitRules : []
});

/**
Expand Down Expand Up @@ -1026,12 +1027,7 @@ bemDom = /** @exports */{

base = getEntityBase(Block, blockName, base);

var lazyInit = extractLazyInitProp(staticProps),
entity = bem.declBlock(blockName, base, props, staticProps);

lazyInit !== null && processLazyInitRule(entity, lazyInit);

return entity;
return bem.declBlock(blockName, base, props, staticProps);
},

/**
Expand All @@ -1054,12 +1050,7 @@ bemDom = /** @exports */{

base = getEntityBase(Elem, entityName, base);

var lazyInit = extractLazyInitProp(staticProps),
entity = bem.declElem(blockName, elemName, base, props, staticProps);

lazyInit !== null && processLazyInitRule(entity, lazyInit);

return entity;
return bem.declElem(blockName, elemName, base, props, staticProps);
},

declMixin : bem.declMixin,
Expand Down
21 changes: 21 additions & 0 deletions common.blocks/i-bem-dom/i-bem-dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,27 @@ describe('i-bem-dom', function() {
});
});

it('should use force init priority on domNode between multiple mods', function() {
spy = sinon.spy();

var Block = bemDom.declBlock('block', {
onSetMod : { js : { inited : spy } }
}, {
lazyInit : false
});

Block.declMod({ modName : 'm1' }, null, { lazyInit : false });
Block.declMod({ modName : 'm2' }, null, { lazyInit : true });

rootNode = initDom([
{ block : 'block', mods : { m1 : true, m2 : true }, js : true },
{ block : 'block', mods : { m1 : true }, js : true },
{ block : 'block', mods : { m2 : true }, js : true },
]);

spy.should.have.calledTwice;
});

it('should be possible to force initialization', function() {
spy = sinon.spy();

Expand Down

0 comments on commit 18487b0

Please sign in to comment.