Components 101
- Each component has a DOM element that contains information to be shown to the user
- Each component has an
initialize function to set up the component
- Each component has a
render function that is used to update/display content to the user
- A component should be contained and be able to know about itself (by checking
attributes)
var component = {
domElement: null,
initialize: function(selector) {
this.domElement = document.createElement('div');
selector.appendChild(this.domElement);
},
render: function(statusText) {
this.domElement.innerHTML = statusText;
}
};
User Component Example
var user = {
name: null,
score: 0,
domElement: null,
initialize: function(elementToAppendTo) {
if (this.name == null) {
this.name = prompt('What is your name?');
}
this.domElement = document.createElement('div');
elementToAppendTo.appendChild(this.domElement);
console.log('initialize: complete');
},
render: function(innerHTML) {
if (typeof(innerHTML) == 'string') {
this.domElement.innerHTML = innerHTML;
}
},
buildPlayerStatusString: function() {
return this.name + ': ' + this.score;
},
getName: function() {
return this.name;
},
saveName: function(newName) {
if (typeof(newName) == 'string' && newName.length > 0) {
this.name = newName;
} else {
alert('You entered an incorrect or empty name');
}
},
getScore: function() {
return this.score;
},
updateScoreByOnePoint: function() {
this.score = this.score + 1;
var status = this.buildPlayerStatusString();
this.render(status);
return this.score;
}
};
Another Example
var comp = {
domElement: null,
initialize: function(domSelector) {
console.log('initializing component');
this.domElement = document.createElement('img');
domSelector.appendChild(this.domElement);
},
render: function(imageSrc) {
this.domElement.src = imageSrc;
}
};
var body = document.getElementsByTagName('body')[0];
console.log(body);
body.innerHTML = '';
var ponyImg = 'http://www.animalsbase.com/wp-content/uploads/2015/06/Pony.jpg';
comp.initialize(body);
comp.render(ponyImg);