Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember - observes vs. property clash

Tags:

ember.js

I have the following props in my Controller

App.TeamController = Ember.ObjectController.extend(
  involvedProjectTeams: (->
    return @get("content.projectTeams").filter (projectTeam, index, enumerable) ->
      projectTeam.get("sdeScopingWeeks") isnt 0
  ).property("[email protected]")
  notInvolvedProjectTeams: (->
    return @get("content.projectTeams").filter (projectTeam, index, enumerable) ->
      return projectTeam.get("sdeScopingWeeks") is 0
  ).observes("[email protected]")
)

I then iterate over both involvedProjectTeams as well as notInvolvedProjectTeams in the underlying template. I am getting the following error:

Uncaught TypeError: Object function () {
      return this.get("content.projectTeams").filter(function(projectTeam, index, enumerable) {
        return projectTeam.get("sdeScopingWeeks") === 0;
      });
    } has no method 'addArrayObserver' 

Why does property() work as expected but observes throws an error?

Thanks!!

like image 631
Dragan Avatar asked Jan 30 '14 10:01

Dragan


People also ask

How do I observe a computed property in Ember?

Ember supports observing any property, including computed properties. You can set up an observer on an object by using the observes method on a function: Because the fullName computed property depends on firstName , updating firstName will fire observers on fullName as well. Observers in Ember are currently synchronous.

How do I define inline observers in Emberjs?

You can define inline observers by using the Ember.observer method if you are using Ember without prototype extensions: You can also add observers to an object outside of a class definition using addObserver:

Are observers in Ember synchronous?

Observers in Ember are currently synchronous. This means that they will fire as soon as one of the properties they observe changes. Because of this, it is easy to introduce bugs where properties are not yet synchronized: This synchronous behaviour can also lead to observers being fired multiple times when observing multiple properties:

How do I set up an observer on a computed property?

Old Guides - You are viewing the guides for Ember v1.10.0 . VIEW v3.28.0 Ember supports observing any property, including computed properties. You can set up an observer on an object by using the observes method on a function: Because the fullName computed property depends on firstName , updating firstName will fire observers on fullName as well.


1 Answers

From Ember's guides

In a nutshell, computed properties let you declare functions as properties. You create one by defining a computed property as a function, which Ember will automatically call when you ask for the property. You can then use it the same way you would any normal, static property.

So if you want to access something in the template, it should be a property.

Observers will just return the function only, so that can't be accessed in the templates. This is the reason why you get an error while accessing notInvolvedProjectTeams, which is just a function not a property.

Define this also as a computed property, so that you can access them in the templates.

P.S: You can use, ember's reduceComputed to define these properties.

like image 98
Hyder Avatar answered Sep 21 '22 23:09

Hyder