Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions blocks/i-bem-node/i-bem-node.common.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
expect(g.done).to.be.an('function');
});
if (global.document) {

describe('client tests', function () {
it('js true for blocks with init', function () {
BN.addDecl('tets-block-js-true').instanceProp({
Expand All @@ -32,6 +33,39 @@
expect(block).to.be.an('object');
expect(block.testField).to.be.equal('testField');
});

it('simple use with bem.json', function () {
//outer bem.json
BEM.JSON.decl('mix-use-outer-bem-json-block', {
onBlock: function (ctx) {
ctx.content({
block: 'mix-use-bem-bn-block'
});
}
});

//inner bem.json
BEM.JSON.decl('mix-use-inner-bem-json-block', {
onBlock: function (ctx) {
ctx.content('inner-content');
}
});

//inner bn
BN.addDecl('mix-use-bem-bn-block').blockTemplate(function (ctx) {
ctx.content({
block: 'mix-use-inner-bem-json-block'
});
}).done();

var $node = jQuery(BN('i-content').html({
block: 'mix-use-outer-bem-json-block'
}, true));

expect($node.hasClass('mix-use-outer-bem-json-block')).to.be.equal(true);
expect($node.find('.mix-use-bem-bn-block .mix-use-inner-bem-json-block').text()).to.be.equal('inner-content');

});
});
}
it('temaplte params', function () {
Expand Down
120 changes: 120 additions & 0 deletions blocks/i-bem/__json/_bh/i-bem__json_bh_yes.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* render bem.json decls via bh
*/
(function (decl, bh, Ctx) {

/**
* replace Ctx._params null with []
* @param {BEM.JSON._ctx} ctx
* @return {Bemjson}
*/
var getResult = function (ctx) {
return ctx._params || [];
};

/**
* bh matcher for bem.json
* @param {BH.Ctx} bhCtx
* @return {Bemjson|Promise.<{Bemjson}>}
*/
var matcher = function (bhCtx) {
var p,
node = bhCtx.node,
json = bhCtx.json(),
position = bhCtx.position(),
thread = {
count: 0,
callback: function () {}
},
bemJsonCtx = new Ctx(
json,
position,
node.index === 'content' ? position : node.arr.length,
json.elem ? {block: json.block, mods: json.blockMods} : undefined,
bhCtx.tParam()
);
bemJsonCtx._globalThread = thread;
bemJsonCtx._threads = 0;
bemJsonCtx.build();
if (thread.count === 0) {
//sync build
return getResult(bemJsonCtx);
} else {
//async build
p = Vow.promise();
thread.callback = function () {
p.fulfill(getResult(bemJsonCtx));
};
return p;
}
};

/**
* defined matcherss
* @type {Object.<{String} expression, {true}>}
*/
var matchers = {};

/**
* add bh matcher once for each expression
* @param {String} expr
*/
var match = function (expr) {
if (!matchers[expr]) {
matchers[expr] = true;
bh.match(expr, matcher);
}
};

/**
* do not render contend with bem.json
* @override
*/
Ctx.prototype._buildInner = function (params) {
return params;
};

/**
* @override
*/
Ctx.prototype.tParam = function () {
throw new Error('tParam in not supported');
};

/**
* @override
*/
BEM.JSON.buildAsync = function () {
throw new Error('BEM.JSON.buildAsync deprecated, use bh.processBemJsonAsync');
};

/**
* @override
*/
BEM.JSON.build = function () {
throw new Error('BEM.JSON.build deprecated, use bh.processBemJson');
};

/**
* add bh matchers for bem.jspn decls
* @override
*/
BEM.JSON.decl = function (desc, props) {
var name = typeof desc === 'string' ? desc : desc.name;

if (props.onBlock) {
match(name);
}
if (props.onElem) {
if (typeof props.onElem === 'function') {
throw new Error('BEM.JSON onElem = func is not supported');
}
if (typeof props.onElem === 'object') {
Object.keys(props.onElem).forEach(function (elem) {
match(name + '__' + elem);
});
}
}
return decl.apply(this, arguments);
};
}(BEM.JSON.decl, BEM.blocks['i-bh'].bh(), BEM.JSON._ctx));
Loading