Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| richclient:emberjs:modelobjet:computed [2018/01/20 18:18] – [Computed properties] jcheron | richclient:emberjs:modelobjet:computed [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 34: | Ligne 34: | ||
| Dans ce cas, la propriété **fullName** sera automatiquement mise à jour sur les changements de **firstName** ou **lastName**. | Dans ce cas, la propriété **fullName** sera automatiquement mise à jour sur les changements de **firstName** ou **lastName**. | ||
| + | |||
| + | ===== Affectation : setter ===== | ||
| + | Il est possible de faire en sorte qu'une computed property soit modifiable :\\ | ||
| + | Il suffit dans ce cas de définir sa méthode **set** : | ||
| + | |||
| + | <sxh javascript> | ||
| + | import EmberObject, | ||
| + | |||
| + | Person = EmberObject.extend({ | ||
| + | firstName: null, | ||
| + | lastName: null, | ||
| + | |||
| + | fullName: computed(' | ||
| + | get(key) { | ||
| + | return `${this.get(' | ||
| + | }, | ||
| + | set(key, value) { | ||
| + | let [firstName, lastName] = value.split(/ | ||
| + | this.set(' | ||
| + | this.set(' | ||
| + | return value; | ||
| + | } | ||
| + | }) | ||
| + | }); | ||
| + | |||
| + | |||
| + | let captainAmerica = Person.create(); | ||
| + | captainAmerica.set(' | ||
| + | captainAmerica.get(' | ||
| + | captainAmerica.get(' | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Macros ===== | ||
| + | |||
| + | Certain types de computed properties sont très communs et disposent de macros permettant d' | ||
| + | |||
| + | Le cas suivant illustre la computed property macro **equals** : | ||
| + | <sxh javascript> | ||
| + | import EmberObject, | ||
| + | import { equal } from ' | ||
| + | |||
| + | Person = EmberObject.extend({ | ||
| + | fullName: 'Tony Stark', | ||
| + | |||
| + | isIronManLongWay: | ||
| + | return this.get(' | ||
| + | }), | ||
| + | |||
| + | isIronManShortWay: | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | |< 100% >| | ||
| + | ^Macro ^Exemple ^Rôle ^ | ||
| + | |and |<sxh javascript; | ||
| + | |bool |<sxh javascript; | ||
| + | |collect |<sxh javascript; | ||
| + | |empty |<sxh javascript; | ||
| + | |equal |<sxh javascript; | ||
| + | |filter |<sxh javascript; | ||
| + | remainingChores: | ||
| + | return !chore.get(' | ||
| + | }) | ||
| + | });</ | ||
| + | |filterBy |<sxh javascript; | ||
| + | remainingChores: | ||
| + | });</ | ||
| + | |||
| + | |||