Skip to content

Commit 18487b0

Browse files
committed
Force init priority between mods
1 parent f711cfd commit 18487b0

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

common.blocks/i-bem-dom/i-bem-dom.js

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function initEntity(entityName, domElem, params, ignoreLazyInit, callback) {
153153

154154
entityCls._processInit();
155155

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

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

344344
/**
345-
* Extract lazyInit property from staticProps
345+
* Extract lazyInit property from static props
346346
* @param {Object} [staticProps]
347347
* @returns {?Boolean}
348348
*/
@@ -359,20 +359,16 @@ function extractLazyInitProp(staticProps) {
359359
/**
360360
* Processing lazyInit rules for entity
361361
* @param {Function} entity BemDomEntity
362-
* @param {Object} [mod] mod declaration
362+
* @param {Object} mod mod declaration
363363
* @param {Boolean} lazyInit lazyInit behavior
364364
* @returns {?Boolean}
365365
*/
366366
function processLazyInitRule(entity, mod, lazyInit) {
367-
if(arguments.length < 3) {
368-
lazyInit = mod;
369-
mod = undef;
370-
}
371-
372-
var rules = entity._lazyInitRules || (entity._lazyInitRules = []);
367+
var rules = entity._lazyInitRules;
373368

374369
rules.push({
375-
check : mod? entity._buildModValRE(mod.modName, mod.modVal) : entity._buildRE(),
370+
check : entity._buildModValRE(mod.modName, mod.modVal),
371+
modName : mod.modName,
376372
lazyInit : lazyInit
377373
});
378374
}
@@ -864,15 +860,6 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{
864860
return this.getEntityName() + MOD_DELIM + modName;
865861
},
866862

867-
/**
868-
* Builds a regular expression for check entity on DOM element
869-
* @private
870-
* @returns {RegExp}
871-
*/
872-
_buildRE : function() {
873-
return new RegExp('(\\s|^)' + this.getEntityName() + '?(?=\\s|$)');
874-
},
875-
876863
/**
877864
* Builds a regular expression for extracting modifier values from a DOM element of an entity
878865
* @private
@@ -914,18 +901,28 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{
914901
/**
915902
* Check domNode for lazy initialization entity
916903
* @protected
917-
* @returns {?Boolean}
904+
* @param {HTMLElement} domNode
905+
* @returns {Boolean|undefined}
918906
*/
919-
_lazyInitCheck : function(domNode) {
920-
var rules = this._lazyInitRules, rule;
921-
if(!rules) return null;
907+
_checkLazyInit : function(domNode) {
908+
var rules = this._lazyInitRules,
909+
len = rules.length,
910+
rule,
911+
lazyInit,
912+
modName;
922913

923-
var len = rules.length;
924914
while(rule = rules[--len]) {
925-
if(rule.check.test(domNode.className)) return rule.lazyInit;
915+
if(modName !== rule.modName && rule.check.test(domNode.className)) {
916+
if(lazyInit === undef || lazyInit === true) {
917+
modName = rule.modName;
918+
lazyInit = rule.lazyInit;
919+
}
920+
921+
if(lazyInit === false) return lazyInit;
922+
}
926923
}
927924

928-
return null;
925+
return lazyInit !== undef? lazyInit : this.lazyInit;
929926
}
930927
});
931928

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

945944
/**
@@ -953,6 +952,8 @@ var Elem = inherit([bem.Elem, BemDomEntity], /** @lends Elem.prototype */{
953952
_block : function() {
954953
return this._blockInstance || (this._blockInstance = this.findParentBlock(getEntityCls(this.__self._blockName)));
955954
}
955+
}, {
956+
_lazyInitRules : []
956957
});
957958

958959
/**
@@ -1026,12 +1027,7 @@ bemDom = /** @exports */{
10261027

10271028
base = getEntityBase(Block, blockName, base);
10281029

1029-
var lazyInit = extractLazyInitProp(staticProps),
1030-
entity = bem.declBlock(blockName, base, props, staticProps);
1031-
1032-
lazyInit !== null && processLazyInitRule(entity, lazyInit);
1033-
1034-
return entity;
1030+
return bem.declBlock(blockName, base, props, staticProps);
10351031
},
10361032

10371033
/**
@@ -1054,12 +1050,7 @@ bemDom = /** @exports */{
10541050

10551051
base = getEntityBase(Elem, entityName, base);
10561052

1057-
var lazyInit = extractLazyInitProp(staticProps),
1058-
entity = bem.declElem(blockName, elemName, base, props, staticProps);
1059-
1060-
lazyInit !== null && processLazyInitRule(entity, lazyInit);
1061-
1062-
return entity;
1053+
return bem.declElem(blockName, elemName, base, props, staticProps);
10631054
},
10641055

10651056
declMixin : bem.declMixin,

common.blocks/i-bem-dom/i-bem-dom.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,27 @@ describe('i-bem-dom', function() {
17291729
});
17301730
});
17311731

1732+
it('should use force init priority on domNode between multiple mods', function() {
1733+
spy = sinon.spy();
1734+
1735+
var Block = bemDom.declBlock('block', {
1736+
onSetMod : { js : { inited : spy } }
1737+
}, {
1738+
lazyInit : false
1739+
});
1740+
1741+
Block.declMod({ modName : 'm1' }, null, { lazyInit : false });
1742+
Block.declMod({ modName : 'm2' }, null, { lazyInit : true });
1743+
1744+
rootNode = initDom([
1745+
{ block : 'block', mods : { m1 : true, m2 : true }, js : true },
1746+
{ block : 'block', mods : { m1 : true }, js : true },
1747+
{ block : 'block', mods : { m2 : true }, js : true },
1748+
]);
1749+
1750+
spy.should.have.calledTwice;
1751+
});
1752+
17321753
it('should be possible to force initialization', function() {
17331754
spy = sinon.spy();
17341755

0 commit comments

Comments
 (0)