KISSY.use('base, node, event', function(S, Base, Node, Event) { var $ = Node.all; // 自定义类 // 继承 Base var Person = Base.extend({ sayHi: function(name) { alert("Hi, " + name + "! Nice to meet you!"); //触发自定义事件 this.fire('say'); }, tellName: function() { alert("My name is " + this.get('name') + "."); this.fire('say'); }, tellAge: function() { alert("I'm " + this.get('age') + " years old."); this.fire('say'); }, introduce: function(extra) { alert("Hello everyone, my name is " + this.get('name') + ", and I'm " + this.get('age') + " years old." + extra); this.fire('say'); } },{ ATTRS: { name: { //设置默认值 value: "" }, age: { value: 1, }, gender: { value: Gender.MALE, /* 指定setter和getter方法的操作 */ setter: function(value) { if ( S.isString(value) ) { return Gender[value.toUpperCase()]; } return value; }, getter: function(value) { return S.keys(Gender)[value]; }, } } }); window.person = new Person({ name: "Andy", age: 18, gender: "female" }); /* 继承Person */ var Student = Person.extend({ introduce: function() { //调用父类方法 this.callSuper("I'm a student of " + this.get('school') + ".My hometown is " + this.get('from') + "."); }, addScore: function(subject, score) { //error异常收集 //设置子属性 this.set('score.' + subject, score, { error: function(e) { console.log(e); } }); }, /* bind属性变化的方法 */ _onSetScore: function(value, ev) { if (!S.isEmptyObject(value)) { alert('Score change'); alert(this.get('score')); } } }, { ATTRS: { school: { value: "" }, from: { value: "" }, score: { } } }); var Teacher = Person.extend({ introduce: function() { this.callSuper("I'm a " + this.get('level') + "teacher, and I teach " + this.get('teach') + "."); }, upLevel: function() { this.set('level', this.get('level') + 1, { error: function(e) { alert(e); } }); }, downLevel: function() { this.set('level', this.get('level') - 1, { error: function(e) { alert(e); } }); } }, { ATTRS: { teach: { value: "" }, level: { value: Level.PRIMARY, setter: function(value) { if ( S.isString(value) ) { return Level[value.toUpperCase()]; } return value; } } } });
|