Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In javascript the only way to do some processing when getting or setting one property is using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty" rel="nofollow noreferrer">Object.defineProperty</a>:</p> <pre><code>Object.defineProperty(person, "b", { get : function() { return person.firstName + ' ' + person.surname; }, set : function(newValue) { var names = newValue.split(' '); person.firsname = names[0]; person.surname = names[1]; }, enumerable : true, configurable : true }); </code></pre> <p>But this have some disadvantages:</p> <ul> <li>Isn't cross browser</li> <li>Doesn't have binding, in other words, if <code>firstname</code> or <code>surname</code> changes, the dependent property <code>fullname</code> isn't changed.</li> <li>Calling <code>person.name</code> when <code>person</code> is undefined, make an error to be throwed</li> <li>Isn't possible to trigger observers, no without additional code and aware of the depency hierachy: <code>firstname</code> depends from <code>fullname</code>, and it can be dependency of others properties arghhh!</li> </ul> <p>Due to this Ember has the concept of "property", called <a href="http://emberjs.com/guides/object-model/computed-properties/" rel="nofollow noreferrer">computed property</a>.</p> <p>It can be declared in 2 ways:</p> <pre><code>foo: Ember.computed(function({ ... }).property(dependent keys); </code></pre> <p>or when using (the default) <code>Ember.ENV.EXTEND_PROTOTYPES = true</code>:</p> <pre><code>foo: function() { ... }.property(dependent keys); </code></pre> <p>The <code>property(dependent keys)</code>, is needed because it tell to ember what is the properies that when changed, will make the property be updated.</p> <pre><code>fullname: function(key, value) { // setter if (value !== undefined) { var names = value.split(' '); this.set('firstname', names[0]); this.set('surname', names[1]); } // always return the complete result, so nexts calls to this.get('fullname') will return the cached value return this.get('firstname') + ' ' + this.get('surname'); }.property('firstname', 'surname') </code></pre> <p>Using this, you have the advantage of:</p> <ul> <li>when changing <code>firstname</code> or <code>surname</code> to a new value, <code>fullname</code> is changed. </li> <li>The <code>beforeObserves</code> are triggered before changing the value, and the <code>observes</code> are triggered after the value change.</li> <li>Any template referencing some property is updated</li> <li>More then one call to person.get('firstname'), will return a cached value, saving processing. You can disable this using <code>.property(..).volatile()</code></li> <li>Avoid null or undefined errors, when accessing null objects like: <code>controller.get('person.dog.name')</code> returns <code>undefined</code>, if person or dog is undefined.</li> </ul> <p>I hope it helps</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload