Todos.TodoController = Ember.ObjectController.extend({ isCompleted: function(key, value){ var model = this.get('model'); if (value === undefined) { // property being used as a getter return model.get('isCompleted'); } else { // property being used as a setter model.set('isCompleted', value); model.save(); return value; } }.property('model.isCompleted') });
I'm working through the ToDo guide for Ember.js and I can't seem to understand how this controller works. What does the .property() mean? And how come when I remove the 'return value;' line the functionality stays the same. If someone could explain exactly what's going on here that would be great.
Link to the guide: http://emberjs.com/guides/getting-started/marking-a-model-as-complete-incomplete/
Property functions are calls to . NET Framework methods that appear in MSBuild property definitions. Unlike tasks, property functions can be used outside of targets.
Return: Returns a property attribute from the given getter, setter and deleter. Note: If no arguments are given, property() method returns a base property attribute that doesn't contain any getter, setter or deleter. If doc isn't provided, property() method takes the docstring of the getter function.
The property() method in Python provides an interface to instance attributes. It encapsulates instance attributes and provides a property, same as Java and C#. The property() method takes the get, set and delete methods as arguments and returns an object of the property class.
The @property decorator is a built-in decorator in Python for the property() function. Use @property decorator on any method in the class to use the method as a property. You can use the following three decorators to define a property: @property: Declares the method as a property.
In javascript the only way to do some processing when getting or setting one property is using Object.defineProperty:
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 });
But this have some disadvantages:
firstname
or surname
changes, the dependent property fullname
isn't changed.person.name
when person
is undefined, make an error to be throwedfirstname
depends from fullname
, and it can be dependency of others properties arghhh!Due to this Ember has the concept of "property", called computed property.
It can be declared in 2 ways:
foo: Ember.computed(function({ ... }).property(dependent keys);
or when using (the default) Ember.ENV.EXTEND_PROTOTYPES = true
:
foo: function() { ... }.property(dependent keys);
The property(dependent keys)
, is needed because it tell to ember what is the properies that when changed, will make the property be updated.
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')
Using this, you have the advantage of:
firstname
or surname
to a new value, fullname
is changed. beforeObserves
are triggered before changing the value, and the observes
are triggered after the value change..property(..).volatile()
controller.get('person.dog.name')
returns undefined
, if person or dog is undefined.I hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With