Object Model Setting Computed Properties

setting computed

The setting of computed properties can be done with the Setter and Getter methods. This manages the values of the variable declared in the computed property. The set() method evaluates values for a certain condition specified in the program and the get() method gets the values from the setter and displays the data.

Syntax

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

Example

The following example sets and gets the values of variable declared in the computed property and shows how to display the data −

import Ember from 'ember';

export default function() {
   var Person = Ember.Object.extend ({
      firstName: null,
      lastName: null,
      fullName: Ember.computed('firstName', 'lastName', function() {
         return this.get('firstName') + this.get('lastName');
      })
   });
   
   var nameDetails = Person.create();
   nameDetails.set('fullName', "Steve Smith");
   nameDetails.get('firstName');   // Steve
   nameDetails.get('lastName');    // Smith
   document.write("<h3>Full Name of the Person:<br><h3>" + nameDetails.get('fullName'));
}

Now open the app.js file and add the following line at the top of the file −

import settingcomputedproperties from './settingcomputedproperties';

Where, the settingcomputedproperties is a name of the file specified as “settingcomputedproperties.js” and created under the “app” folder. Now, call the inherited “settingcomputedproperties” at the bottom, before the export. It executes the settingcomputedproperties function which is created in the settingcomputedproperties.js file −

settingcomputedproperties();

Output

Run the ember server and you will receive the following output −

setting computed

Previous Page:-Click Here

Leave a Reply