监听事件

    1. var App = Intact.extend({
    2. template: template,
    3. defaults: function() {
    4. return {count: 0};
    5. },
    6. onClick: function(e) {
    7. this.set('count', this.get('count') + 1);
    8. }
    9. });
    10. Intact.mount(App, document.getElementById('appevent'));

    利用bind方法,我们可以往事件处理函数传递参数。

    1. var App = Intact.extend({
    2. template: template,
    3. },
    4. onClick: function(num, e) {
    5. this.set('count', this.get('count') + num);
    6. }
    7. });
    8. Intact.mount(App, document.getElementById('appevent1'));
    1. var App = Intact.extend({
    2. template: template,
    3. onClick: function(e) {
    4. e.preventDefault();
    5. }
    6. });
    7. Intact.mount(App, document.getElementById('appevent2'));

    监听组件事件

    绑定组件暴露的事件,和原生事件一样,例如:

    1. var Component = Intact.extend({
    2. onClick: function() {
    3. this.trigger('increase');
    4. }
    5. });
    6. var App = Intact.extend({
    7. template: template,
    8. defaults: {
    9. count: 0
    10. },
    11. _init: function() {
    12. this.Component = Component;
    13. },
    14. add: function() {
    15. this.set('count', this.get('count') + 1);
    16. }
    17. });
    18. Intact.mount(App, document.getElementById('appevent3'));

    @since v2.2.0 无需bind(self),默认会指向绑定事件的实例