Emberjs-obj-mod-chaining-computed-properties

提供:Dev Guides
移動先:案内検索

EmberJS-オブジェクトモデルチェーン計算プロパティ

チェーン計算プロパティは、単一の*プロパティ*の下で1つ以上の定義済み計算プロパティと集約するために使用されます。

構文

var ClassName = Ember.Object.extend ({
   NameOfComputedProperty1: Ember.computed(function() {
      return VariableName;
   }),

   NameOfComputedProperty2: Ember.computed(function() {
      return VariableName;
   });
});

次の例は、計算プロパティを値として使用して、新しい計算プロパティを作成する方法を示しています-

import Ember from 'ember';

export default function() {
   var Person = Ember.Object.extend ({
      firstName: null,
      lastName: null,
      age: null,
      mobno: null,

     //Defining the Details1 and Details2 computed property function
      Details1: Ember.computed('firstName', 'lastName', function() {
         return this.get('firstName') + ' ' + this.get('lastName');
      }),

      Details2: Ember.computed('age', 'mobno', function() {
         return 'Name: ' + this.get('Details1') + '<br>' + ' Age: ' + this.get('age') +
            '<br>' + ' Mob No: ' + this.get('mobno');
      }),
   });

   var person_details = Person.create ({
     //initializing the values for variables
      firstName: 'Jhon',
      lastName: 'Smith',
      age: 26,
      mobno: '1234512345'
   });

   document.write("<h2>Details of the Person: <br></h2>");
  //displaying the values by get() method
   document.write(person_details.get('Details2'));
}

今_app.js_ファイルを開き、ファイルの先頭に次の行を追加します-

import chainingcomputedproperties from './chainingcomputedproperties';

ここで、 chainingcomputedproperties は、「chainingcomputedproperties.js」として指定され、「app」フォルダーの下に作成されたファイルの名前です。

次に、エクスポートの前に、下部の継承された「chainingcomputedproperties」を呼び出します。 chainingcomputedproperties.jsファイルで作成されたchainingcomputedproperties関数を実行します-

chainingcomputedproperties();

出力

エンバーサーバーを実行すると、次の出力が表示されます-

Ember.js計算プロパティのチェーン