简体   繁体   English

MVC-将数据从视图传递到控制器

[英]MVC - Passing data from view to controller

I'm relatively knew to web development and I was assigned to a web project that I recently started. 我比较了解Web开发,因此我被分配到一个最近开始的Web项目。 To put you in palce, I'm using webpack to bundle my javascript + html + css files and I want to follow MVC. 为了使您感到舒服,我正在使用webpack捆绑我的javascript + html + css文件,并且我想关注MVC。

My problem comes at the start at the login, and as I have just started the project, I don't mind changing the structure if needed. 我的问题来自登录时的开始,正如我刚刚启动该项目一样,如果需要,我不介意更改结构。

The thing is that I have a main.js which requires loginController.js which renders loginView.js, a basic login form, so loginController requires loginView, but when the user clicks the submit button, which function is loginView, I need to tell somehow the loginController to access the loginModel for data and return it. 关键是我有一个main.js,它需要loginController.js来呈现基本的登录表单loginView.js,所以loginController需要loginView,但是当用户单击Submit按钮时,该函数是loginView,我需要以某种方式进行说明loginController可以访问loginModel获取数据并返回它。 So my loginController requires loginView, and my loginView requires loginController, which I believe causes a circular dependency, either way webpack seems not to be able to compile properly my code like that. 所以我的loginController需要loginView,而我的loginView需要loginController,我相信这会导致循环依赖,无论哪种方式,webpack似乎都无法正确地编译我的代码。

Obviously I'm doing something wrong, maybe the structure since the beggining or some obvious code is placed where it shouldn't be. 显然我做错了,也许是因为开始的结构或一些明显的代码放在了不应该的地方。 I've googled for a few days, read more about MVC, read a bunch of questions in SO but I can't find the answer, maybe I'm blind. 我已经搜索了几天,阅读了有关MVC的更多信息,在SO中阅读了一堆问题,但我找不到答案,也许我是盲人。

How can I solve this? 我该如何解决? The controller needs to know when the submit button is pressed, but that function is in the view, but the view needs to connect the controller to ask the model for data and return it. 控制器需要知道何时按下了提交按钮,但是该功能在视图中,但是视图需要连接控制器以向模型询问数据并返回模型。
Any help or guidance willl be kindly appreciated. 任何帮助或指导将不胜感激。

loginController.js loginController.js

var loginViewAux = require('./loginView.js');
var loginModelAux = require('./loginModel.js');
var LoginView, LoginModel;

LoginController = function() {
    LoginView = new loginViewAux();
    LoginModel = new loginModelAux();

}
LoginController.prototype.render = function() {
    LoginView.render();
};
LoginController.prototype.setEvents = function() {
    LoginView.setEvents();
}
LoginController.prototype.login = function(username, password) {
    return LoginModel.loginPost(username, password);
};

module.exports = LoginController;

loginView.js loginView.js

require('./login.css');
var loginTemplate = require('./loginTemplate.html');
var logo = require('./../img/ttlvd-color-alborde.png');
var LoginController = require('./loginController.js');


LoginView = function() {}

LoginView.prototype.render = function() {
    $('#loginContainer').html(loginTemplate);
    $('#loginLogo').attr("src", logo);
}

LoginView.prototype.setEvents = function() {
    $('#btnSubmit').on('click', function(e) {
        var username, password;
        e.preventDefault();

        username = $('#loginFormUsername').html();
        password = $('#loginFormPassword').html();
        var result = LoginController.login(username, password);
        console.log(result);
    });
}

module.exports = LoginView;

More info: 更多信息:
A main.js loads loginController 一个main.js加载loginController

main.js main.js

require('./style.css');
var loginControllerAux = require('./app/login/loginController.js');
var LoginController = new loginControllerAux();

LoginController.setEvents();
LoginController.render();

I haven't tried it out yet. 我还没有尝试过。 But as far as I am used to it is good if the Controller only knows the View and Model and acts as a communicator between these two. 但据我所知,如果Controller仅了解View和Model并充当这两者之间的沟通者,那就很好。

Obviously the View has to interact with the Controller. 显然,视图必须与Controller交互。 If the Controller is on the backend side this often happens with HTTP requests. 如果Controller在后端,则HTTP请求经常会发生这种情况。 But also an Event driven approach is possible. 但是事件驱动的方法也是可能的。 How about firing Events in the login View and the Controller subscribes to them and acts accordingly 如何在登录视图中触发事件,然后Controller订阅它们并采取相应措施

since you are using purely objects in your stated code an event-driven approach does not directly work here. 由于您仅在声明的代码中使用对象,因此事件驱动方法无法在此处直接使用。 You would need something like a message broker where components can subscribe to and send messages with. 您将需要像消息代理之类的组件可以用来订阅和发送消息的消息。

another approach would be not to require eg the login view and login controller but just require it in main js and pass the controller to the view or vice versa in main.js. 另一种方法是不需要例如登录视图和登录控制器,而仅在main js中需要它,并将控制器传递给视图,在main.js中反之亦然。 Your choice to decide how clean this is. 您可以选择这有多干净。

Callback function approach: 回调函数方法:

LoginView: 登录视图:

LoginView.prototype.setLoginEvent = function(callback) {
    $('#btnSubmit').on('click', function(e) {
        var username, password;
        e.preventDefault();

        username = $('#loginFormUsername').html();
        password = $('#loginFormPassword').html();
        var result = callback(username, password);
        console.log(result);
    });
}

LoginController: LoginController:

LoginController.prototype.setEvents = function() {
    LoginView.setLoginEvent(this.login);
}

Here the login function of the controller gets passed to the login view and will be called when the button submits. 此处,控制器的登录功能将传递到登录视图,并在提交按钮时调用。 This way the View has not to require the Controller 这样,视图就不需要控制器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM