Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

demo #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions .enb/techs/bemtreeToHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
* ```
*/
var vm = require('vm'),
path = require('path'),
vow = require('vow'),
vfs = require('enb/lib/fs/async-fs'),
asyncRequire = require('enb/lib/fs/async-require'),
dropRequireCache = require('enb/lib/fs/drop-require-cache');
dropRequireCache = require('enb/lib/fs/drop-require-cache'),
dataFilename = path.resolve('data.json');

module.exports = require('enb/lib/build-flow').create()
.name('html-from-bemtree')
Expand All @@ -29,10 +31,11 @@ module.exports = require('enb/lib/build-flow').create()
.useSourceFilename('bemhtmlTarget', '?.bemhtml.js')
.builder(function(bemtreeFilename, bemhtmlFilename) {
dropRequireCache(require, bemhtmlFilename);
dropRequireCache(require, dataFilename);

return vow.all([
vfs.read(bemtreeFilename, 'utf-8'),
asyncRequire(bemhtmlFilename)
asyncRequire(bemhtmlFilename),
])
.spread(function(bemtree, bemhtml) {
var ctx = vm.createContext({
Expand All @@ -46,7 +49,7 @@ module.exports = require('enb/lib/build-flow').create()
return [ctx.BEMTREE, bemhtml.BEMHTML];
})
.spread(function(BEMTREE, BEMHTML) {
return BEMTREE.apply({ block: 'root' })
return BEMTREE.apply({ block: 'root', data: require(dataFilename) })
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

сейчас это технология, которая выполняется при enb make и, соответственно, через enb server, но по смыслу ее логика должна быть реализована в продакшен-сервере:

  1. прочитать файлы собранных шаблонов
  2. получить данные (например, из БД)
  3. вызвать apply шаблонов и передать в них необходимые данные

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ВОТ! Вот из таких семплов, как dataFilename, и надо бандлы собирать! Ура!!!

.then(function(bemjson) {
return BEMHTML.apply(bemjson);
});
Expand Down
3 changes: 3 additions & 0 deletions common.blocks/main/main.bemhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
block('main')(
tag()('form')
);
33 changes: 33 additions & 0 deletions common.blocks/main/main.bemtree
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
block('main').content()(function() {
var cars = this.data.cars,
carNames = Object.keys(cars),
firstCar = carNames[0],
firstCarModels = Object.keys(cars[firstCar]);

return [
{
block : 'select',
mods : { mode : 'radio', theme : 'islands', size : 'xl' },
mix : { block: this.block, elem: 'car' },
name : 'car',
val : firstCar,
options : carNames.map(function(car) {
return { val : car, text : car };
})
},
{
block : 'select',
mods : { mode : 'radio', theme : 'islands', size : 'xl' },
mix : { block: this.block, elem: 'model' },
name : 'model',
val : firstCarModels[0],
options : firstCarModels.map(function(model) {
return { val : model, text : model };
})
},
{
block: 'modal',
mods: { theme: 'islands', autoclosable : true }
}
];
});
9 changes: 9 additions & 0 deletions common.blocks/main/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.main
{
padding: 20px;
}

.main .select
{
margin-right: 12px;
}
23 changes: 23 additions & 0 deletions common.blocks/main/main.deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
shouldDeps : [
'objects',
{
block : 'select',
mods : { mode : 'radio', theme : 'islands', size : 'xl' }
},
{
block: 'modal',
mods: { theme: 'islands', autoclosable : true }
}
]
},
{
tech : 'js',
shouldDeps : {
block : 'select',
mods : { mode : 'radio', theme : 'islands', size : 'xl' },
tech : 'bemhtml'
}
}
]
49 changes: 49 additions & 0 deletions common.blocks/main/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
modules.define('main', ['i-bem__dom', 'BEMHTML', 'objects', 'select'], function(provide, BEMDOM, BEMHTML, Objects, Select) {

provide(BEMDOM.decl(this.name, {
onSetMod: {
js: {
inited: function() {
var cars = this.params.cars,
modal = this.findBlockInside('modal'),
car = cars[Object.keys(cars)[0]];

Select.on(this.elem('car'), 'change', function(e) {
var select = e.target,
val = select.getVal();

car = cars[val];

if (Objects.isEmpty(car)) {
return BEMDOM.update(this.elem('model'), '');
}

BEMDOM.update(this.elem('model'), BEMHTML.apply({
block : 'select',
mods : { mode : 'radio', theme : 'islands', size : 'xl' },
mix : { block: this.block, elem: 'model' },
name : 'model',
val : car[0],
options : Object.keys(car).map(function(model) {
return { val : model, text : model };
})
}));
}, this);

Select.on(this.elem('model'), 'change', function(e) {
var model = e.target.getVal(),
colors = car[model].colors;

modal
.setContent('<p>Варианты цветов</p>' +
(Object.keys(colors).map(function(color) {
return color + ': ' + colors[color];
}).join('<br>')))
.setMod('visible');
}, this);
}
}
}
}));

});
4 changes: 4 additions & 0 deletions common.blocks/modal/modal.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.modal__content
{
padding: 12px;
}
3 changes: 2 additions & 1 deletion common.blocks/page/page.bemtree
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ block('page').content()(function() {
block: 'header'
},
{
block: 'main'
block: 'main',
js: this.data
},
{
block: 'footer'
Expand Down
2 changes: 2 additions & 0 deletions common.blocks/root/root.bemtree
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
block('root').def()(function() {
this.data = this.ctx.data;

return applyCtx({
block: 'page',
title: 'TODO',
Expand Down
32 changes: 32 additions & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"cars" : {
"toyota" : {
"prius" : {
"colors" : {
"c1" : "qwe",
"c2" : "asd"
}
},
"camry" : {
"colors" : {
"c1" : "123",
"c2" : "456"
}
}
},
"mercedes" : {
"benz" : {
"colors" : {
"c1" : "black",
"c2" : "red"
}
},
"blah" : {
"colors" : {
"c1" : "white",
"c2" : "silver"
}
}
}
}
}