I'm trying to create a simple counter app using MVC approach and I haven't been able to figure out why the event listener for the down counter does not work but the up counter does. There has got to be something that I am overlooking but I am totally new to this approach. any help would be greatly appreciated.
//model
var model =
{
clickCount: 0
};
//controller
var controller = {
init: function() {
view.init();
},
incrementCounterUp: function() {
counter.textContent = model.clickCount += 1;
},
incrementCounterDown: function() {
counter.textContent = model.clickCount -= 1;
}
};
//view
var counter = document.createElement('p');
counter.textContent = (model.clickCount);
counter.id = counter;
document.body.appendChild(counter);
var upButton = document.createElement("button");
upButton.id = upButton;
document.body.appendChild(upButton);
var upButtonText = document.createTextNode("Up");
upButtonText.id = upButtonText;
upButton.appendChild(upButtonText);
var downButton = document.createElement("button");
downButton.id = downButton;
document.body.appendChild(downButton);
var downButtonText = document.createTextNode("Down");
downButtonText.id = downButtonText;
downButton.appendChild(downButtonText);
var view = {
init: function() {
// store pointers to our DOM elements for easy access
this.counterElem = document.getElementById(counter);
this.upButtonElem = document.getElementById(upButton);
this.upButtonElem.addEventListener('click', function(){
controller.incrementCounterUp();
});
this.downButtonElem = document.getElementById(downButton);
this.downButtonElem.addEventListener('click', function(){
controller.incrementCounterDown();
});
}
};
controller.init();
contoller.incrementCounterDown();
You misspelled "controller". :P
//model
var model =
{
clickCount: 0
};
//controller
var controller = {
init: function() {
view.init();
},
incrementCounterUp: function() {
model.clickCount += 1
view.render()
},
incrementCounterDown: function() {
model.clickCount -= 1
view.render()
}
};
//view
var counter = document.createElement('p');
counter.textContent = (model.clickCount);
counter.id = 'counter';
document.body.appendChild(counter);
var upButton = document.createElement("button");
upButton.textContent = 'Up'
upButton.id = 'upButton';
document.body.appendChild(upButton);
var downButton = document.createElement("button");
downButton.textContent = 'Down';
downButton.id = 'downButton';
document.body.appendChild(downButton);
var view = {
init: function() {
// store pointers to our DOM elements for easy access
this.upButtonElem = document.getElementById('upButton')
this.downButtonElem = document.getElementById('downButton')
this.downButtonElem.addEventListener('click', function(){
controller.incrementCounterDown()
})
this.upButtonElem.addEventListener('click', function(){
controller.incrementCounterUp()
})
},
render: function() {
this.counterElem = document.getElementById('counter')
this.counterElem.textContent = model.clickCount
}
};
controller.init();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.