Friday, February 17, 2017

EmberJS - Object Model

Object oriented analysis and design technique is called as object modeling. In Ember.js, all objects are derived from the Ember.Object.

Classes and Instances

Class is a template or blue print, that has collection of variables and functions, where as instances are related to the object of that class.
You can create new Ember class by using Ember.Object's extend() method as shown below:
App.NameOfClass = Ember.Object.extend({
   VariableName1:'values',
   ..
   VariableNamen:'values',
   FunctionName: function(thing)
   {
      //display logic
   }
});
NameOfClass and FunctionName are the name of the class and name of the function which are defined in your class.
Let's see how to extend the child class by using base class as shown below:
App.Student = Ember.Object.extend({
   disp: function() {
      var roll = this.get('rollnum');
      var name = this.get('name');
      document.write("Roll num: "+roll+" Name: "+name);
   }
});

App StudentInfo = App.Student.extend({
   rollnum:"12",
   name: "Jhon",
});
In the above code, name of the base class is Student and StudentInfo is child class, which is extended by the base class. The disp is the name of the function declared in the base class, which displays the student information. You can use the _super() method to immediately refer the parent class methods.
The following table show how to create the instance of an Ember class:
S.N.Instance & Description
1 Creating and Initializing Instances
Creates the instantiate of that class by calling the create() method and uses init() method to automatically initialize an instance of the class.
Let us see some more details about object model by clicking the below links:

When to Use Computed Properties, Observers and Bindings

  • Computed properties declare functions as properties. You can create one by defining a computed property as a function which ember.js will automatically call when you ask for the property. Multiple calls to the computed properties will always return the same values.
  • The Observer pattern is used if there is relationship between two or more objects such as if one object is updated, its dependent objects are to be notified automatically.
  • Bindings helps to combine the two different layers to make synchronous.

No comments:

Post a Comment