Skip to content

Commit 10e1660

Browse files
committed
Merge pull request #9 from tejitak/translate/guide
Translate custom directive & custom filter section in guide to Japanese
2 parents 23ed2b1 + 5150e94 commit 10e1660

File tree

2 files changed

+75
-72
lines changed

2 files changed

+75
-72
lines changed

source/guide/custom-directive.md

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
title: Custom Directives
1+
title: カスタムディレクティブ
22
type: guide
33
order: 9
44
---
55

6-
## The Basics
6+
## 基本
77

8-
Vue.js allows you to register custom directives, essentially enabling you to teach Vue new tricks on how to map data changes to DOM behavior. You can register a global custom directive with the `Vue.directive(id, definition)` method, passing in a **directive id** followed by a **definition object**. A definition object can provide several hook functions (all optional):
8+
Vue.js ではカスタムディレクティブを登録する仕組みが用意されています。カスタムディレクティブはデータの変更に伴い DOM がどのように変更されるかを定義することができる仕組みです。**directive id** とそれに続く **definition object**`Vue.directive(id, definition)`メソッドに渡して、グローバルにカスタムディレクティブを登録することができます。この definition object はいくつかの hook 関数(全て任意)を提供します:
99

10-
- **bind**: called only once, when the directive is first bound to the element.
11-
- **update**: called for the first time immediately after `bind` with the initial value, then again whenever the binding value changes. The new value and the previous value are provided as the argument.
12-
- **unbind**: called only once, when the directive is unbound from the element.
10+
- **bind**: ディレクティブが初めて対象のエレメントに紐付いた時に一度だけ呼ばれる
11+
- **update**: 初めの一度は bind の直後に初期値とともに呼ばれ、以降、バインディングされている値が変更される度に呼ばれる。引数には新しい値と以前の値が渡される
12+
- **unbind**: ディレクティブが紐付いているエレメントから取り除かれた時に一度だけ呼ばれる
1313

14-
**Example**
14+
****
1515

1616
``` js
1717
Vue.directive('my-directive', {
1818
bind: function () {
19-
// do preparation work
20-
// e.g. add event listeners or expensive stuff
21-
// that needs to be run only once
19+
// 準備のための作業をします
20+
// e.g. イベントリスナーを追加したり、一回だけ実行が必要なコストのかかる処理を行う
2221
},
2322
update: function (newValue, oldValue) {
24-
// do something based on the updated value
25-
// this will also be called for the initial value
23+
// 更新された値に何か処理をします
24+
// この部分は初期値に対しても呼ばれます
2625
},
2726
unbind: function () {
28-
// do clean up work
29-
// e.g. remove event listeners added in bind()
27+
// クリーンアップのための処理を行います
28+
// e.g. bind()の中で追加されたイベントリスナーの削除
3029
}
3130
})
3231
```
3332

34-
Once registered, you can use it in Vue.js templates like this (you need to add the Vue.js prefix to it):
33+
一度登録された後は、以下のように Vue.js のテンプレート内で使用することができます (Vue.js prefix が必要です):
3534

3635
``` html
3736
<div v-my-directive="someValue"></div>
3837
```
3938

40-
When you only need the `update` function, you can pass in a single function instead of the definition object:
39+
`update`関数のみが必要な場合は、definition object の代わりに関数を一つ渡すこともできます:
40+
4141

4242
``` js
4343
Vue.directive('my-directive', function (value) {
44-
// this function will be used as update()
44+
// この関数は update() として使用される
4545
})
4646
```
4747

48-
All the hook functions will be copied into the actual **directive object**, which you can access inside these functions as their `this` context. The directive object exposes some useful properties:
48+
全ての hook 関数は実際の **directive object** にコピーされます。directive object は hook 関数の内側で `this` のコンテキストとしてアクセスすることができます。この directive object はいくつかの便利なプロパティを持っています:
4949

50-
- **el**: the element the directive is bound to.
51-
- **vm**: the context ViewModel that owns this directive.
52-
- **expression**: the expression of the binding, excluding arguments and filters.
53-
- **arg**: the argument, if present.
54-
- **raw**: the raw, unparsed expression.
55-
- **name**: the name of the directive, without the prefix.
50+
- **el**: ディレクティブが紐づく要素
51+
- **vm**: このディレクティブを所有する ViewModel
52+
- **expression**: 引数とフィルタ以外のバインディングの expression
53+
- **arg**: 引数(もしある場合)
54+
- **raw**: 元のパースされる前の expression
55+
- **name**: prefix 無しのディレクティブの名前
5656

57-
<p class="tip">You should treat all these properties as read-only and refrain from changing them. You can attach custom properties to the directive object too, but be careful not to accidentally overwrite existing internal ones.</p>
57+
<p class="tip">これらの全てのプロパティは read-only で変更しないものとして扱わなくてはいけません。カスタムプロパティを directive object に追加することができますが、意図せずに既存のインターナルなプロパティを上書きしないように注意が必要です。</p>
5858

59-
An example of a custom directive using some of these properties:
59+
いくつかのプロパティを使用したカスタムディレクティブの例:
6060

6161
``` html
6262
<div id="demo" v-demo="LightSlateGray : msg"></div>
@@ -85,7 +85,7 @@ var demo = new Vue({
8585
})
8686
```
8787

88-
**Result**
88+
**結果**
8989

9090
<div id="demo" v-demo="LightSlateGray : msg"></div>
9191
<script>
@@ -111,11 +111,11 @@ var demo = new Vue({
111111
})
112112
</script>
113113

114-
## Literal Directives
114+
## リテラルディレクティブ
115115

116-
If you pass in `isLiteral: true` when creating a custom directive, the attribute value will be taken as a literal string and assigned as that directive's `expression`. The directive will not attempt to setup data observation.
116+
もしカスタムディレクティブを作成するときに `isLiteral: true` を渡した場合は、その属性値は文字列 string として扱われ、そのディレクティブの `expression` として割り当てられます。リテラルディレクティブはデータの監視の準備はしません。
117117

118-
Example:
118+
:
119119

120120
``` html
121121
<div v-literal-dir="foo"></div>
@@ -130,29 +130,32 @@ Vue.directive('literal-dir', {
130130
})
131131
```
132132

133-
### Dynamic Literal
133+
### 動的リテラル
134+
135+
しかし、リテラルディレクティブに mustache タグを含んでいる場合は、以下のような挙動になります:
136+
137+
138+
- ディレクティブインスタンスは `this._isDynamicLiteral` というフラグを `true` にセットします。
134139

135-
However, in the case that the literal directive contains mustache tags, the behavior is as follows:
140+
- もし `update` function が提供されていない場合、 mustache 表現は一度だけ評価され、 `this.expression` に割り当てられます。データ監視は行われません。
136141

137-
- The directive instance will have a flag `this._isDynamicLiteral` set to `true`;
142+
- もし `update` function が提供される場合、ディレクティブはその expression に対するデータ監視をセットアップし、評価された結果が変更される度に `update` が呼ばれます。
138143

139-
- If no `update` function is provided, the mustache expression will be evaluated only once and assigned to `this.expression`. No data observation happens.
144+
## 双方向ディレクティブ
140145

141-
- If an `update` function is provided, the directive **will** setup data observation for that expression and call `update` when the evaluated result changes.
142146

143-
## Two-way Directives
147+
もしディレクティブが受け取ったデータを Vue インスタンスに書き戻したい場合は `twoWay: true` を渡す必要があります。このオプションはディレクティブの `this.set(value)` で使用することができます。
144148

145-
If your directive expects to write data back to the Vue instance, you need to pass in `twoWay: true`. This option allows the use of `this.set(value)` inside the directive:
146149

147150
``` js
148151
Vue.directive('example', {
149152
twoWay: true,
150153
bind: function () {
151154
this.handler = function () {
152-
// set data back to the vm.
153-
// If the directive is bound as v-example="a.b.c",
154-
// this will attempt to set `vm.a.b.c` with the
155-
// given value.
155+
// vm にデータをセットします
156+
// もしディレクティブが v-example="a.b.c" と紐付いている場合,
157+
// 与えられた値を `vm.a.b.c`
158+
// セットしようと試みます
156159
this.set(this.el.value)
157160
}.bind(this)
158161
this.el.addEventListener('input', this.handler)
@@ -163,9 +166,9 @@ Vue.directive('example', {
163166
})
164167
```
165168

166-
## Inline Statements
169+
## インラインステートメント
167170

168-
Passing in `acceptStatement:true` enables your custom directive to accept inline statements like `v-on` does:
171+
`acceptStatement:true` を渡すことでカスタムディレクティブが `v-on` が行っているようなインラインステートメントを使用できるようになります:
169172

170173
``` html
171174
<div v-my-directive="a++"></div>
@@ -175,18 +178,18 @@ Passing in `acceptStatement:true` enables your custom directive to accept inline
175178
Vue.directive('my-directive', {
176179
acceptStatement: true,
177180
update: function (fn) {
178-
// the passed in value is a function which when called,
179-
// will execute the "a++" statement in the owner vm's
180-
// scope.
181+
// 呼び出される際に渡される値は function です
182+
// function は "a++" ステートメントを
183+
// 所有者の vm のスコープで実行します
181184
}
182185
})
183186
```
184187

185-
Use this wisely though, because in general you want to avoid side-effects in your templates.
188+
ただし、テンプレート内のサイドエフェクトを避けるためにも、賢く使いましょう。
186189

187-
## Deep Observation
190+
## ディープ監視
188191

189-
If your custom directive is expected to be used on an Object, and it needs to trigger `update` when a nested property inside the object changes, you need to pass in `deep: true` in your directive definition.
192+
もしカスタムディレクティブでオブジェクトを扱いたい場合で、オブジェクトの内側のネストされたプロパティが変更された時に `update` をトリガーしたい場合は、ディレクティブの定義に `deep: true` を渡す必要があります。
190193

191194
``` html
192195
<div v-my-directive="obj"></div>
@@ -196,16 +199,16 @@ If your custom directive is expected to be used on an Object, and it needs to tr
196199
Vue.directive('my-directive', {
197200
deep: true,
198201
update: function (obj) {
199-
// will be called when nested properties in `obj`
200-
// changes.
202+
// `obj` の中のネストされたプロパティが
203+
// 変更された時に呼ばれる
201204
}
202205
})
203206
```
204207

205-
## Directive Priority
208+
## ディレクティブの優先度
206209

207-
You can optionally provide a priority number for your directive (defaults to 0). A directive with a higher priority will be processed earlier than other directives on the same element. Directives with the same priority will be processed in the order they appear in the element's attribute list, although that order is not guaranteed to be consistent in different browsers.
210+
ディレクティブには任意で優先度の数値 (デフォルトは0) を与えることができます。同じ要素上で高い優先度をもつディレクティブは他のディレクティブより早く処理されます。同じ優先度をもつディレクティブは要素上の属性のリストに出現する順番で処理されますが、ブラウザが異なる場合、一貫した順番になることは保証されません。
208211

209-
You can checkout the priorities for some built-in directives in the [API reference](/api/directives.html). Additionally, `v-repeat`, `v-if` and `v-component` are considered "terminal directives" and they always have the highest priority in the compilation process.
212+
いくつかのビルトインディレクティブに関する優先度は [API reference](/api/directives.html) で確認できます。さらに `v-repeat``v-if` `v-component` は "ターミナルディレクティブ" として扱われ、コンパイル処理の中で常に最も高い優先度を持ちます。
210213

211-
Next, we'll see how to [write a custom filter](/guide/custom-filter.html).
214+
次は [カスタムフィルタ](/guide/custom-filter.html) をどのように書くか見ていきましょう。

source/guide/custom-filter.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
title: Custom Filters
1+
title: カスタムフィルタ
22
type: guide
33
order: 10
44
---
55

6-
## The Basics
6+
## 基本
77

8-
Similar to custom directives, you can register a custom filter with the global `Vue.filter()` method, passing in a **filterID** and a **filter function**. The filter function takes a value as the argument and returns the transformed value:
8+
カスタムディレクティブと同様に、グローバルな `Vue.filter()` を使用してカスタマイズしたフィルタを登録することができます。引数には **filterID** **filter function** を渡します。このフィルタの関数は引数として値を受け取って、変換した値を返します:
99

1010
``` js
1111
Vue.filter('reverse', function (value) {
@@ -18,7 +18,7 @@ Vue.filter('reverse', function (value) {
1818
<span v-text="message | reverse"></span>
1919
```
2020

21-
The filter function also receives any inline arguments:
21+
フィルタ関数はインラインの引数を受け取ることもできます:
2222

2323
``` js
2424
Vue.filter('wrap', function (value, begin, end) {
@@ -31,34 +31,34 @@ Vue.filter('wrap', function (value, begin, end) {
3131
<span v-text="message | wrap before after"></span>
3232
```
3333

34-
## Two-way Filters
34+
## 双方向フィルタ
3535

36-
Up till now we have used filters to transform values coming from the model and before displaying them in the view. But it is also possible to define a filter that transforms the value before it is written back to the model from the view (input elements):
36+
これまでフィルタはモデルから渡される値をビューに表示される前に変換するために使用していました。しかし、input 要素などのビューからモデルに書き込みがされる前に値を変換するフィルタの定義も可能です。
3737

3838
``` js
3939
Vue.filter('check-email', {
40-
// read is optional in this case, it's here
41-
// just for demo purposes.
40+
// read は任意のもので、
41+
// ここではデモ用に記載します
4242
read: function (val) {
4343
return val
4444
},
45-
// this will be invoked before writing to
46-
// the model.
45+
// モデルへの書き出しが行われる前に
46+
// 呼び出されます
4747
write: function (val, oldVal) {
4848
return isEmail(val) ? val : oldVal
4949
}
5050
})
5151
```
5252

53-
## Filter Context
53+
## フィルタコンテキスト
5454

55-
When a filter is invoked, its `this` context is set to the Vue instance that is invoking it. This allows it to output dynamic results based on the state of the owner Vue instance.
55+
フィルタが呼び出される時、`this` のコンテキストにはそれを呼び出している Vue インスタンスがセットされます。これによって、その所有者である Vue インスタンスの状態に応じて動的な結果を出力することが可能になります。
5656

57-
For example:
57+
:
5858

5959
``` js
6060
Vue.filter('concat', function (value, key) {
61-
// `this` points to the Vue instance invoking the filter
61+
// `this` はフィルタを呼び出す Vue インスタンスを指します
6262
return value + this[key]
6363
})
6464
```
@@ -67,8 +67,8 @@ Vue.filter('concat', function (value, key) {
6767
<span>{{msg | concat userInput}}</span>
6868
```
6969

70-
For this simple example above, you can achieve the same result with just an expression, but for more complicated procedures that need more than one statements, you need to put them either in a computed property or a custom filter.
70+
上記の簡単な例では、 expression をそのまま記述した時と同じ結果が得られます。しかし、複数のステートメントが必要な複雑な処理においては、Computed Property もしくは カスタムフィルタが必要になります。
7171

72-
The built-in `filterBy` and `orderBy` filters are both filters that perform non-trivial work on the Array being passed in and relies on the current state of the owner Vue instance.
72+
ビルトインの `filterBy` `orderBy` フィルタは共に渡された配列に対して重要な変更を行うものであり、所有者である Vue インスタンスの現在の状態に依存するものです。
7373

74-
Alright! Now it's time to learn how the [Component System](/guide/components.html) works.
74+
以上!これで次は [Component System](/guide/components.html) がどのように動作するか学ぶ時間です。

0 commit comments

Comments
 (0)