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 [2018/01/20 16:22] – jcheron | richclient:emberjs:modelobjet [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 14: | Ligne 14: | ||
const Person = EmberObject.extend({ | const Person = EmberObject.extend({ | ||
- | name:'', | ||
say(thing) { | say(thing) { | ||
alert(thing); | alert(thing); | ||
- | }, | ||
- | toString(){ | ||
- | return $this.get(' | ||
} | } | ||
}); | }); | ||
+ | </ | ||
+ | |||
+ | ===== Instanciation ===== | ||
+ | L' | ||
+ | |||
+ | <sxh javascript> | ||
+ | let person = Person.create(); | ||
+ | person.say(' | ||
+ | </ | ||
+ | |||
+ | La méthode create accepte en paramètre un objet js permettant d' | ||
+ | |||
+ | <sxh javascript> | ||
+ | import EmberObject from ' | ||
+ | |||
+ | const Person = EmberObject.extend({ | ||
+ | helloWorld() { | ||
+ | alert(`Hi, my name is ${this.get(' | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | let tom = Person.create({ | ||
+ | name: 'Tom Dale' | ||
+ | }); | ||
+ | |||
+ | tom.helloWorld(); | ||
+ | </ | ||
+ | |||
+ | ===== Héritage/ | ||
+ | |||
+ | * L' | ||
+ | * La surdéfinition par l' | ||
+ | |||
+ | <sxh javascript> | ||
+ | import EmberObject from ' | ||
+ | |||
+ | const Person = EmberObject.extend({ | ||
+ | say(thing) { | ||
+ | alert(`${this.get(' | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | const Soldier = Person.extend({ | ||
+ | say(thing) { | ||
+ | // this will call the method in the parent class (Person# | ||
+ | // the string ', sir!' to the variable `thing` passed in | ||
+ | this._super(`${thing}, | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | let yehuda = Soldier.create({ | ||
+ | name: ' | ||
+ | }); | ||
+ | |||
+ | yehuda.say(' | ||
</ | </ | ||
===== Initialisation/ | ===== Initialisation/ | ||
+ | Quand un objet est instancié, la méthode **init()** de sa classe est invoquée automatiquement. | ||
+ | <sxh javascript> | ||
+ | import EmberObject from ' | ||
- | ===== Instanciation | + | const Person |
+ | init() { | ||
+ | alert(`${this.get(' | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | Person.create({ | ||
+ | name: ' | ||
+ | }); | ||
+ | |||
+ | // alerts " | ||
+ | </ | ||
+ | |||
+ | La méthode init permet d' | ||
+ | |||
+ | |||
+ | ===== getters/ | ||
+ | |||
+ | L' | ||
<sxh javascript> | <sxh javascript> | ||
+ | import EmberObject from ' | ||
+ | |||
+ | const Person = EmberObject.extend({ | ||
+ | name: ' | ||
+ | }); | ||
+ | |||
let person = Person.create(); | let person = Person.create(); | ||
- | person.say('Hello'); // alerts " says: Hello" | + | |
+ | person.get('name'); // ' | ||
+ | person.set(' | ||
+ | person.get(' | ||
</ | </ | ||
+ | |||