Skip to content

Instantly share code, notes, and snippets.

@qfox
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qfox/45c35ac395dfec326555 to your computer and use it in GitHub Desktop.
Save qfox/45c35ac395dfec326555 to your computer and use it in GitHub Desktop.
elem-inst-test
({
block : 'page',
title : 'elem-inst-test',
styles : [{ elem : 'css', url : 'elem-inst-test.min.css' }],
scripts : [{ elem : 'js', url : 'elem-inst-test.min.js' }],
content : [
{
block : 'elem-inst-test',
js : true,
mods : { mod : true },
content : [
{
elem : 'elem1',
content : [
{
block : 'input',
mix : { block : 'elem-inst-test', elem : 'elem2', mods : { type : 'input' } }
}
]
}
]
}
]
})
// block
modules.define('elem-inst-test', ['i-bem__dom', 'objects'], function(provide, BEMDOM, objects) {
provide(BEMDOM.decl(this.name, {
onSetMod : {
'js' : {
'inited' : function() {
console.log('@root _js_inited, self.getName(): ', this.__self.getName());
console.log('@root elem1.value', this.elemInstance('elem1').value);
console.log('@root elem2.value', this.elemInstance('elem2').value);
console.log('@root .value', this.value);
}
}
}
}));
});
// block__elem1
modules.define('elem-inst-test', function(provide, Block) {
provide(Block.decl({ block : this.name, elem : 'elem1' }, {
onSetMod : {
'js' : {
'inited' : function() {
console.log('@__elem1 _js_inited, self.getName(): ', this.__self.getName());
console.log('@__elem1 .block().value: ', this.block().value);
console.log('@__elem1 .value: ', this.value);
}
}
},
value : '!!!!!!! it\'s elem1!'
}));
});
// block__elem2
modules.define('elem-inst-test', function(provide, Block) {
provide(Block.decl({ block : this.name, elem : 'elem2' }, {
onSetMod : {
'js' : {
'inited' : function() {
console.log('@__elem2 _js_inited, self.getName(): ', this.__self.getName());
console.log('@__elem2 .block().value: ', this.block().value);
console.log('@__elem2 .value: ', this.value);
}
}
}
}));
});
({
mustDeps : {
block : 'i-bem',
elems : [{ name : 'dom', mods : ['elem-instances'] }]
},
shouldDeps : [
'objects'
]
})
console.log:
@root _js_inited, self.getName(): elem-inst-test
@__elem1 _js_inited, self.getName(): elem-inst-test__elem1
@__elem1 .block().value: undefined
@__elem1 .value: !!!!!!! it's elem1!
@root elem1.value !!!!!!! it's elem1!
@__elem2 _js_inited, self.getName(): elem-inst-test__elem2
@__elem2 .block().value: undefined
@__elem2 .value: !!!!!!! it's elem1!
@root elem2.value !!!!!!! it's elem1!
@root .value undefined
@aristov
Copy link

aristov commented Feb 4, 2015

You have such behavior, because you provide different objects as the same module:

BEMDOM.decl('elem-inst-test', { /*...*/ }); // => BEMDOM.blocks['elem-inst-test']
Block.decl({ elem : 'elem1' }, { /*...*/ }); // => BEMDOM.blocks['elem-inst-test__elem1']

So you should define element as separate module:

modules.define('elem-inst-test__elem1', ['i-bem__dom'], function(provide, BEMDOM) {
    provide(BEMDOM.decl({ block : 'elem-inst-test', elem : 'elem1' }, { /*...*/ });
});

or declare element, but provide block:

modules.define('elem-inst-test', function(provide, Block) {
    Block.decl({ elem : 'elem1' }, { /*...*/ });
    provide(Block);
});

@qfox
Copy link
Author

qfox commented Feb 4, 2015

@aristov ;-) Yep. Thanks.

What about:

var Block = BEMDOM.decl('bbb', { x : 1 });
provide(Block.decl({ modName : 'x' }, { x : 2 })); // ?

Should I make separate module for that? ;-)

Nvm, Just docs here: bem/bem-core#429 (comment) are confusing.

@aristov
Copy link

aristov commented Feb 9, 2015

No, you shouldn't. When you declare modifier of block or element, you get the same BEM-entity with additional behavior. When you declare element, you get another BEM-entity. I agree that this moment is unclear. This problem will be solved in bem-core@v3 where all elements are BEM-entities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment