這可能是一個簡單而愚蠢的問題,但我在此處不知所措。我們的教授要求我們創建一個使用MVC的遊戲(在我的例子中是Sudoku)。視圖和控制器可以作爲一個js文件使用(因爲我們正在弄溼我們的腳),並且模型必須位於單獨的js文件中。我能夠使視圖工作得很好,但是當我嘗試在模型中使用某些東西時......我不知道如何調用模型文件(數組或包含要輸入的值的81個元素數獨網格)。任何幫助,閱讀或視頻將不勝感激。 謝謝。如何在Javascript中使用MVC實現模型
-1
A
回答
0
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>
相關問題
- 1. MVC - 實現模型
- 2. MVC模型實現?
- 3. 在MVC中實現多狀態模型
- 4. 如何在Tkinter中實現MVC模式
- 5. 在MVC模式中實現模型的最佳實踐
- 6. 如何在JavaScript中實現`使用`?
- 7. 如何在actor模型中實現MVCC
- 8. 如何在MVVM中實現模型
- 9. ASP.Net MVC使用javascript在模型中發佈模型
- 10. 如何使用原型框架在Javascript中實現OO?
- 11. 我如何在Javascript中使用模型?
- 12. 如何使用模型在Javascript(C#ASP.NET MVC)
- 13. 如何使用Javascript實現Javascript/HTML Playground?
- 14. 使用ASP.NET MVC實現Oberver模式
- 15. 使用MVC/Backbone.js實現複合模式
- 16. jqplot和使用asp.net mvc模型呈現
- 17. 如何實現MVC在PHP
- 18. 如何使用網絡API調用在Swift中實現MVC模式?
- 19. 如何在ASP.NET MVC中模擬模型?
- 20. 如何實現MVC
- 21. 如何使用MVC在iOS應用程序中實現設置
- 22. 如何在不違反MVC模式的情況下實現緩存模型?
- 23. Asp.net MVC - 如何實現跨項目使用的靈活模塊?
- 24. 如何在自定義用戶模型中實現「email_user」方法?
- 25. 如何在JPA域模型中實現狀態設計模式
- 26. 如何在uiwebview中實現用於pdf文件實現的javascript?
- 27. 使用SOA架構在Asp.Net MVC中實現存儲庫模式
- 28. 使用Razor MVC在正常模型中播放的IEnumerable模型使用Razor MVC
- 29. 如何在asp.net MVC中實現區域?
- 30. 如何在jQuery中實現MVC
使用angularJs爲MVC –