vendredi 30 janvier 2015

Difference Between Property and Observer?

Why would a computed property in a component not recognize a dependency changed within a view, but does recognize the change as an observer?


I tried two different approaches. The first one, a computed property that depended on the outside variable (favoriteId), only recognized the change some of the time. The other, an observer, sometimes recognized the change around the same time as the computed property, but also recognized it when the computed property did not. For example, it recognized both the property and observer when it inserted the element, only recognized the observer on a page transition within the same route, and only recognized the property on a page transition to a different route. Why is that? From what I understand, a dependency set on a computed property is similar to an observer, so why would it have trouble?


This is the component using the computed property:



import Ember from "ember";

export default Ember.Component.extend({
isFavorite: function() {
console.log("property");
// returns true or false
}.property("favorites", "favoriteId")
});


This is the component with an observer:



import Ember from "ember";

export default Ember.Component.extend({
isFavorite: null,

observesFavorite: function() {
console.log("observer");
// sets isFavorite to true or false
}.observes("favorites", "favoriteId")
});


Why do these two work differently? How would I be able to get the component to recognize the changes to favoriteId all of the time?





Aucun commentaire:

Enregistrer un commentaire