Emberjs-computed-properties
提供:Dev Guides
EmberJS-計算されたプロパティ
計算されたプロパティは関数をプロパティとして宣言し、Ember.jsは必要に応じて計算されたプロパティを自動的に呼び出し、1つ以上のプロパティを1つの変数に結合します。
次の表は、計算されたプロパティのプロパティを示しています-
S.No. | Properties & Description |
---|---|
1 |
計算されたチェーンプロパティは、1つ以上の定義済みの計算されたプロパティと集約するために使用されます。 |
2 |
呼び出されたときに、計算されたプロパティを動的に更新します。 |
3 |
setterおよびgetterメソッドを使用して、計算されたプロパティの設定を支援します。 |
例
次の例では、計算されたプロパティをEmber.objectに追加し、データを表示する方法を示します-
import Ember from 'ember';
export default function() {
var Car = Ember.Object.extend ({
//The values for below variables will be supplied by 'create' method
CarName: null,
CarModel: null,
carDetails: Ember.computed('CarName', 'CarModel', function() {
//returns values to the computed property function 'carDetails'
return ' Car Name: ' + this.get('CarName') + '<br>' +
' Car Model: ' + this.get('CarModel');
})
});
var mycar = Car.create ({
//initializing the values of Car variables
CarName: "Alto",
CarModel: "800",
});
//Displaying the information of the car
document.write("<h2>Details of the car: <br></h2>");
document.write(mycar.get('carDetails'));
}
今_app.js_ファイルを開き、ファイルの先頭に次の行を追加します-
import computedproperties from './computedproperties';
ここで、 computedproperties は、「computedproperties.js」として指定され、「app」フォルダーの下に作成されるファイルの名前です。 次に、エクスポートの前に、下部の継承された「計算されたプロパティ」を呼び出します。 computeproperties.jsファイルに作成されたcomputeproperties関数を実行します-
computedproperties();
出力
エンバーサーバーを実行すると、次の出力が表示されます-