EMBER WORKSHOP: Ember Object Model
Ember Object Model
HTML
<script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://builds.emberjs.com/release/ember.js"></script>
JavaScript
App = Ember.Application.create({});
// CLASSES
// BASE CLASS
App.DefaultPlayer = Em.Object.extend({
init: function () {
this.set('imgProfilePrefix', 'default_');
this.set('imgProfileSuffix', '_profile');
},
name: "Steve", // set here instead of passing in create()
imgName: function (imgType) {
return this.get('imgProfilePrefix') + this.get('name').split(' ').join('_').toLowerCase() + this.get('imgProfileSuffix') + '.' + imgType;
}
});
// EXTEND
App.Player = App.DefaultPlayer.extend({
init: function () {
this._super(); // call super, inherit from the base class
this.set('imgProfilePrefix', 'player_');
},
imgName: function (imgType) {
return this._super(imgType); // call super, inherit from the base class
}
});
// CREATING OBJECTS
var steve = App.DefaultPlayer.create({});
var jdcravens = App.Player.create({ // passing name property
name: "Jesse Cravens"
});
console.log(jdcravens.imgName('jpg'));
console.log(steve.imgName('png'));