无法使第二个事件监听器适用于MVC计数器应用程序

时间:2016-10-14 01:06:09

标签: javascript events model-view-controller javascript-events event-handling

我试图使用MVC方法创建一个简单的计数器应用程序,但我还没有能够弄清楚为什么倒数计数器的事件监听器不起作用,但是计数器确实如此。必须有一些我可以忽视的东西,但我对这种方法完全不熟悉。任何帮助将不胜感激。

//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();

2 个答案:

答案 0 :(得分:0)

contoller.incrementCounterDown();

你拼错了#34;控制器"。 :P

答案 1 :(得分:0)

 //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();