aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/idbrequest/success_event/index.html
blob: dcbd3a9712e6092e93e896863d37f60cbdc3d756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: 'IDBRequest: success event'
slug: Web/API/IDBRequest/success_event
translation_of: Web/API/IDBRequest/success_event
---
<div>{{ APIRef("IndexedDB") }}</div>

<p><code>success</code>イベントは<code>IDBRequest</code>が成功すると着火します。</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">Bubbles</th>
   <td>No</td>
  </tr>
  <tr>
   <th scope="row">Cancelable</th>
   <td>No</td>
  </tr>
  <tr>
   <th scope="row">Interface</th>
   <td>{{domxref("Event")}}</td>
  </tr>
  <tr>
   <th scope="row">Event handler property</th>
   <td><code><a href="/ja/docs/Web/API/IDBRequest/onsuccess">onsuccess</a></code></td>
  </tr>
 </tbody>
</table>

<h2 id="例"></h2>

<p>この例では、データベースをオープンします。その<code>success</code>イベントを<code>addEventListener()</code>でリスンします。</p>

<pre class="brush: js">// データベースをオープンする
const openRequest = window.indexedDB.open('toDoList', 4);

openRequest.onupgradeneeded = (event) =&gt; {
  const db = event.target.result;

  db.onerror = () =&gt; {
    console.log('Error creating database');
  };

  // オブジェクトストアを作成する
  var objectStore = db.createObjectStore('toDoList', { keyPath: 'taskTitle' });

  // オブジェクトストアが保有するデータを定義する
  objectStore.createIndex('hours', 'hours', { unique: false });
  objectStore.createIndex('minutes', 'minutes', { unique: false });
  objectStore.createIndex('day', 'day', { unique: false });
  objectStore.createIndex('month', 'month', { unique: false });
  objectStore.createIndex('year', 'year', { unique: false });
};

openRequest.addEventListener('success', (event) =&gt; {
  console.log('Database opened successfully!');
});

</pre>

<p>下記は同じことを<code>onsuccess</code>イベントハンドラープロパティを使用した例です。</p>

<pre class="brush: js">// データベースをオープンする
const openRequest = window.indexedDB.open('toDoList', 4);

openRequest.onupgradeneeded = (event) =&gt; {
  const db = event.target.result;

  db.onerror = () =&gt; {
    console.log('Error creating database');
  };

  // オブジェクトストアを作成する
  var objectStore = db.createObjectStore('toDoList', { keyPath: 'taskTitle' });

  // オブジェクトストアが保有するデータを定義する
  objectStore.createIndex('hours', 'hours', { unique: false });
  objectStore.createIndex('minutes', 'minutes', { unique: false });
  objectStore.createIndex('day', 'day', { unique: false });
  objectStore.createIndex('month', 'month', { unique: false });
  objectStore.createIndex('year', 'year', { unique: false });
};

openRequest.onsuccess = (event) =&gt; {
  console.log('Database opened successfully!');
};
</pre>

<h2 id="ブラウザの対応">ブラウザの対応</h2>



<p>{{Compat("api.IDBRequest.success_event")}}</p>

<h2 id="関連情報">関連情報</h2>

<ul>
 <li><a href="/ja/docs/IndexedDB/Using_IndexedDB">Using IndexedDB</a></li>
 <li><code><a href="/ja/docs/Web/API/IDBRequest/onsuccess">onsuccess</a></code> event handler property</li>
</ul>