5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ractive.jsでイベントを拾う方法まとめ

Last updated at Posted at 2015-02-23

1. イベントハンドラをテンプレートに設定する方法

template
<div id="container">
  <script id="template" type="text/ractive">
     <div on-click="fire">イベントハンドラを付与</div>
  </script>
</div>
ractive.js
var ractiveModel = new Ractive({
  el: '#container',
  template: '#template'
});

ractiveModel.on('fire', function(event){
  console.log(event);
});

2. イベントハンドラとメソッドをテンプレートに設定する方法

template
<div id="container">
  <script id="template" type="text/ractive">
     <div on-click="fire()">イベントハンドラとメソッドを付与</div>
  </script>
</div>
ractive.js
var ractiveModel = new Ractive({
  el: '#container',
  template: '#template',
  fire: function() {
    console.log(this);
  }
});

3. データバインド中のオブジェクトを監視する方法

template
<div id="container">
  <script id="template" type="text/ractive">
     <div> {{target}} </div>
  </script>
</div>
ractive.js
var ractiveModel = new Ractive({
  el: '#container',
  template: '#template',
  data: {
    target: 'abcdefg'
  }
});

ractiveModel.observe('target', function(value){
  console.log(value);
});

// 値を書き換えた瞬間にイベントが発生する
ractiveModel.set('target', 'xxxxxx');

その他にもあるようなので、把握次第追記します。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?