Bala
Bala

Reputation: 11234

How to reference a model in Ember.js?

I am working through toDoMVC and in the code below, just don't quite understand what this refers in var model = this.get('model');.

Does it refer to Controller or Route. If it is Controller, then where do we declare that Model x works with Controller y?

Todos.TodoController = Ember.ObjectController.extend({
  isCompleted: function(key, value){
    var model = this.get('model');
    ...
  }.property('model.isCompleted')
});

Upvotes: 0

Views: 169

Answers (1)

Microfed
Microfed

Reputation: 2890

I. Usually this variable inside Ember.Object functions refers to an instance of that Ember.Object (an instance of Todos.TodoController in your case).

Example:

App.SomeRoute = Em.Route.extend({
    activate: function() {
        this; // this is App.SomeRoute instance ref.
    }
});

App.SomeController = Em.Controller.extend({
    someMethod: function() {
        this; // this is App.SomeController instance ref.
    }
});

App.SomeView = Em.View.extend({
    someMethod: function() {
        this; // this is App.SomeView instance ref.
    }
});

and so on.

II. this.get('model') is just a property of controller (you can set any value to that property if you want). *Before ember v1.7 model was an Ember.computed.alias to controller's property named content, but this behavior was changed in latest ember's versions.

There is another model property, in Em.Route, it differs from Em.ObjectController.model property. Actually, it's a function invoked in routing (transition) sequence, which returns data, which Em.Route uses to set a controller.model property in setupController hook of Em.Route. So, output of this.get('model') inside of Em.Route's methods will be a function.

P.S. I tried not to screw up with tenses, but most likely I didn't succeed.:) I'am sorry.

Upvotes: 2

Related Questions