Last active
January 30, 2016 18:15
-
-
Save vitkarpov/0e290430b92dfd01ccbd to your computer and use it in GitHub Desktop.
Jblocks Events Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Пусть есть блоки A и B. | |
// Нужно научить блок B реагировать на то, что происходит в жизни A. | |
// Удобно для этого использовать события, чтобы не связывать жестко блоки. | |
// В html | |
<div class="js-a" data-b="A"> | |
<button class="js-button">Нажми меня!</button> | |
</div> | |
<div class="js-b" data-b="B"> | |
<div class="js-message"></div> | |
</div> | |
// в js-e | |
$.jblocks({ | |
name: 'A', | |
events: { | |
'click .js-button': 'onClickButton' | |
}, | |
methods: { | |
onClickButton: function() { | |
this.emit('foo', 'hello, world!'); | |
} | |
} | |
}); | |
$.jblocks({ | |
name: 'B', | |
events: { | |
'b-inited': 'oninit' | |
}, | |
methods: { | |
oninit: function() { | |
var a = $('.js-a').jblocks('get')[0]; | |
// подпишемся | |
a.on('foo', this.onFooEmited.bind(this)); | |
// кешируем контейнер, чтобы удобнее было использовать в методах | |
this.content = this.$node.find('.js-inner'); | |
}, | |
// вторым аргументом прилетает объект, которые прислал блок A | |
onFooEmitied: function(e, message) { | |
// напишем сообщение блока A в блоке B | |
this.content.text(message); | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment