user9903
user9903

Reputation:

In Ember.js, how to use parent model's functions

I have two models:

Parent = Ember.Model.extend({
  hello: function() {
    console.log("parent says hello");
  },
  world: function() {
    console.log("parent says world");
  }
});
Child = Ember.Model.extend(Parent, {
  hello: function() {
    console.log("child says hello");
    // how do I call Parent.hello here?
    // how do I call Parent.world here?
  }
});

I want to call the Parent.hello and Parent.world functions from within the Child.hello function.

How would I go about doing this? Is there a super method I can use or do I just use bind and reference the Parent model class within the Child.hello function?

Upvotes: 2

Views: 1372

Answers (2)

Karl-Johan Sjögren
Karl-Johan Sjögren

Reputation: 17522

You can use this._super() to call the parent method. It's talked about briefly in the Ember guides.

http://guides.emberjs.com/v2.1.0/object-model/classes-and-instances/

To call a method that hasn't been overridden you simple do this.world().

You can see a sample of it here: http://emberjs.jsbin.com/renijuqoko/1/edit?js,console,output

Upvotes: 3

Daniel
Daniel

Reputation: 18682

Nothing fancy, just:

how do I call Parent.hello here?

this._super()

how do I call Parent.world here?

this.world()

Working demo.

Upvotes: 3

Related Questions