Skip to content

Instantly share code, notes, and snippets.

@miripiruni
Last active June 23, 2016 12:53
Show Gist options
  • Save miripiruni/ece794f072e73ba597d142732464f333 to your computer and use it in GitHub Desktop.
Save miripiruni/ece794f072e73ba597d142732464f333 to your computer and use it in GitHub Desktop.
var bemxjst = require('./');
var bemhtml = bemxjst.bemhtml;
// Add templates
var templates = bemhtml.compile(function() {
// true/false в аттрибутах
block('*').def()(function() {
var ret = applyNext();
var ctx = this.ctx;
var attrs = ctx.attrs;
if (attrs) {
Object.keys(attrs).forEach(function(key) {
if (typeof attrs[key] === 'boolean') {
console.warn(
'\nWARNING: boolean attribute value:',
attrs[key],
'in BEMJSON:',
ctx
);
console.warn('Notice what bem-xjst behaviour changed: https://github.com/bem/bem-xjst/releases/tag/v4.3.3');
}
});
}
return ret;
});
// mods для elem (вместо elemMods)
block('*').def()(function() {
var ret = applyNext();
var ctx = this.ctx;
if (ctx.mods && ctx.elem && !ctx.elemMods) {
console.warn(
'\nWARNING: mods for elem in BEMJSON:',
ctx
);
console.warn('Notice what bem-xjst behaviour changed: https://github.com/bem/bem-xjst/releases/tag/v5.0.0');
}
return ret;
});
// присвоения в ctx.mods
// block('*').def()(function() {
// var ret = applyNext();
// var ctx = this.ctx;
// if (this.block === 'mods-test')
// ctx.mods.one = 2;
// return ret;
// });
block('*').def()(function() {
var ret;
var ctx = this.ctx;
var mods = ctx.mods;
if (mods) {
var store = Object.keys(mods).map(function(key) {
return {
key: key,
val: mods[key]
};
});
ret = applyNext();
store.forEach(function(mod) {
var key = mod.key;
if (mods[key] !== mod.val) {
console.warn(
'\nWARNING: looks like someone changed ctx.mods in BEMJSON:',
ctx,
'\nold value of ctx.mod.' + mod.key + ' was ' + mod.val,
'\nNotice that bem-xjst recommend to change this.mods instead of this.ctx.mods in templates'
);
}
});
} else {
ret = applyNext();
}
return ret;
});
});
// Tests
templates.apply([
// basic
{ block: 'a' },
// boolean attributes
{ block: 'b', attrs: { one: true, two: 'true' } },
// mods for elem
{ block: 'c', elem: 'e', mods: { test: 'opa' } },
// присвоения в ctx.mods
{ block: 'mods-test', mods: { one: '1', two: '2' } }
]);
// Result: <div class="b">yay</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment