2015-10-05 3 views
-1

Это, наверное, простой и глупый вопрос, но я здесь не в порядке. Наш профессор попросил нас создать игру (в моем случае Sudoku), где мы используем MVC. Вид и контроллер могут использоваться как один файл js (поскольку мы получаем мокрые ноги), а модель должна находиться в отдельном файле js. Я могу заставить просмотр работать отлично, но когда я пытаюсь использовать что-то в модели ... ну, это то, что я не знаю, как вызвать файл модели (массив или 81 элемент, содержащий значение для входа в Судоку). Любая помощь, чтение или видео будут оценены. Спасибо.Как вы реализуете модель MVC в Javascript

+0

Использование angularJs для связывания двухсторонняя для MVC –

ответ

0

В угловых приложениях, вид объектной модели документа (DOM), контроллеры классы JavaScript, и данные модели хранятся в свойствах объекта.

AngularJs Learning

+0

https://xkcd.com/1343/ – Nils

0

Это то, что я понимаю под MVC в JavaScript. Вероятно, это неправильно.

function Model() { 
 
    this.state=0; 
 
    this.observers=[] 
 
    this.addObserver = function(observer) { 
 
     
 
     // i, the model, have no idea what this observer is. 
 
     this.observers.push(observer); 
 
    } 
 
    this.notifyObservers = function() { 
 
     for (i = 0; i < this.observers.length; i++) { 
 
      
 
      // i, the model, have no idea what this does in the observer. 
 
      this.observers[i].modelChanged(); 
 
     } 
 
    } 
 
    this.doSomethingWithInternalState = function(observer){ 
 
     this.state+=1 
 
     
 
     // i, the model will notify observers when my state changes. 
 
     // They can decide on their own what to do then. 
 
     this.notifyObservers(); 
 
    } 
 
} 
 

 
// That would be views (or mini-models or read-only controllers, whatever). 
 
function Observer() { 
 
    this.init = function(model) { 
 
     this.model=model; 
 
    }; 
 
    this.modelChanged = function(){ 
 
     alert('bazinga'); 
 
    }; 
 
} 
 

 
function SudokuBoard(){ 
 
    this.boardsize=0; 
 
    this.modelChanged = function() { 
 
     if (this.model.state < 10) { 
 
      this.boardsize=this.model.state*20; 
 
      alert(this.boardsize); 
 
     } 
 
    }; 
 
} 
 
SudokuBoard.prototype=new Observer; 
 

 
function MessageWindow(){ 
 
    this.modelChanged = function(){ 
 
     alert('Sudoku is cool'); 
 
    }; 
 
} 
 
MessageWindow.prototype=new Observer; 
 

 
function AnotherGuiElem(){ 
 
    this.bazinga=true; 
 
} 
 
AnotherGuiElem.prototype=new Observer; 
 

 
// that would be a controller. 
 
document.onclick=function(){ 
 
    m.doSomethingWithInternalState(); 
 
    a.bazinga=true; 
 
}; 
 

 
// we have a model, views and one controller, now lets connect everything. 
 
var m = new Model; 
 
var b = new SudokuBoard();b.init(m); 
 
var w = new MessageWindow();w.init(m); 
 
var a = new AnotherGuiElem();a.init(m); 
 
m.addObserver(b); 
 
m.addObserver(w); 
 
m.addObserver(a);
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <title>Test</title> 
 
     <meta charset="utf-8" /> 
 
    </head> 
 
    <body> 
 
     <script src="test.js"></script> 
 
    </body> 
 
</html>

Смежные вопросы