JavaScript MVC 模式

来源:互联网 发布:淘宝儿童女装14岁 编辑:程序博客网 时间:2024/05/01 11:42

Javascript MVC 框架很多,比如backbone.js,ember.js 等等

下面给出两个例子用于解释MVC模式:

第一个是:没有使用mvc模式的:

[html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.  <html xmlns="http://www.w3.org/1999/xhtml">   
  3. <head>   
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
  5.     <title>javascript demo no mvc</title>  
  6. </head>   
  7. <body>   
  8.     <h3>JavaScript no MVC</h3>  
  9.     <div>  
  10.         <select name="" id="setAnimal">  
  11.             <option value="cat">cat</option>  
  12.             <option value="fish">fish</option>  
  13.             <option value="bird">bird</option>  
  14.         </select>  
  15.         <p id="whatDoesThisAnimalDo"></p>  
  16.     </div>  
  17.       
  18.     <script type="text/javascript">   
  19.         document.getElementById('setAnimal').onchange = function(){  
  20.             var thisAnimalDoes;  
  21.             switch(this.value){  
  22.                 case 'cat':   
  23.                     thisAnimalDoes = 'cat meows';  
  24.                     break;  
  25.                 case 'fish':  
  26.                     thisAnimalDoes = 'fish swims';  
  27.                     break;  
  28.                 case 'bird':  
  29.                     thisAnimalDoes = 'bird fies';  
  30.                     break;  
  31.                 default:   
  32.                     thisAnimalDoes = 'wuff?';  
  33.             }  
  34.               
  35.             document.getElementById('whatDoesThisAnimalDo').innerHTML = thisAnimalDoes;  
  36.         };  
  37.     </script>   
  38.       
  39. </body>   
  40. </html>   

第二个例子: 采用mvc模式

[html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.  <html xmlns="http://www.w3.org/1999/xhtml">   
  3. <head>   
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
  5.     <title>javascript demo mvc</title>  
  6. </head>   
  7. <body>   
  8.     <h3>JavaScript simple MVC</h3>  
  9.     <div>  
  10.         <select name="" id="setAnimal">  
  11.             <option value="cat">cat</option>  
  12.             <option value="fish">fish</option>  
  13.             <option value="bird">bird</option>  
  14.         </select>  
  15.         <p id="whatDoesThisAnimalDo"></p>  
  16.     </div>  
  17.       
  18.       
  19.       
  20.     <script type="text/javascript">   
  21.         // controller  
  22.         Animal = {  
  23.             start: function(){  
  24.                 this.view.start();  
  25.             },  
  26.             set: function(animalName){  
  27.                 this.model.setAnimal(animalName);  
  28.             }  
  29.         };  
  30.       
  31.         // model  
  32.         Animal.model = {  
  33.             animalDictionary :{  
  34.                 cat: 'meows',  
  35.                 fish: 'swims',  
  36.                 bird: 'flies'  
  37.             },  
  38.               
  39.             currentAnimal:null,  
  40.               
  41.             setAnimal: function(animalName){  
  42.                 this.currentAnimal = this.animalDictionary[animalName]?animalName:null;  
  43.                 this.onchange();  
  44.             },  
  45.               
  46.             onchange: function(){  
  47.                 Animal.view.update();  
  48.             },  
  49.               
  50.             getAnimalAction: function(){  
  51.                 return this.currentAnimal ? this.currentAnimal + " " + this.animalDictionary[this.currentAnimal] : 'wuff?';  
  52.             }  
  53.         };  
  54.           
  55.           
  56.         // view  
  57.         Animal.view = {  
  58.             start: function(){  
  59.                 document.getElementById('setAnimal').onchange = this.onchange;  
  60.             },  
  61.               
  62.             onchange: function(){  
  63.                 Animal.set(document.getElementById('setAnimal').value);  
  64.             },  
  65.               
  66.             update: function(){  
  67.                 document.getElementById('whatDoesThisAnimalDo').innerHTML = Animal.model.getAnimalAction();  
  68.             }  
  69.         };  
  70.           
  71.         Animal.start();  
  72.     </script>   
  73.       
  74. </body>   
  75. </html>   
本文转载自:http://blog.csdn.net/spring21st/article/details/7852179
0 0