这可能是一个简单而愚蠢的问题,但我在此处不知所措。我们的教授要求我们创建一个使用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在JavaScript中实现框架
- 4. 在MVC中实现多状态模型
- 5. 在JavaScript中使用MVC模型
- 6. 如何在Tkinter中实现MVC模式
- 7. 实现MVC模型部分
- 8. 如何使用CakePHP模型实现FriendShip?
- 9. 如何在MVC模型中使用Webservice
- 10. 如何在Java中为MVC模型实现撤销/重做?
- 11. 在MVC模式中实现模型的最佳实践
- 12. 如何在JavaScript中实现`使用`?
- 13. 如何在actor模型中实现MVCC
- 14. 如何在MVVM中实现模型
- 15. ASP.Net MVC使用javascript在模型中发布模型
- 16. 如何使用原型框架在Javascript中实现OO?
- 17. 如何在JavaScript中实现原子操作(并发模型)?
- 18. 我如何在Javascript中使用模型?
- 19. 如何在JavaScript中实现此Java模式(使用继承)?
- 20. 如何用Javascript更新MVC模型clientside?
- 21. 如何在iOS中使用Objective-C实现模型?
- 22. Symfony如何实现模型
- 23. 在rails中实现模型的模型
- 24. MVC - null模型中的对象,如何使用此模型呈现文本框
- 25. 如何使用模型在Javascript(C#ASP.NET MVC)
- 26. 如何使用模型int数组变量在JavaScript MVC 4
- 27. 在JavaScript中如何实现?
- 28. 如何在JavaScript中实现?
- 29. 如何实现在管理和模型
- 30. 如何实现在Zend框架模型
使用angularJs为MVC –