如何将JMVC(javascript-mvc)和服务器端MVC结合在一起

前几天我问过这个问题,没有人回答。


我问过 它也在 forum.javascriptMVC.com </ code>中,现在我有了答案,但我需要更多的想法。</ p>

问题:</ p>
\ n

 我读了javascriptMVC的文档,我喜欢它。

但我不知道如何在大型项目中使用它。
</ code> </ pre>

我认为在服务器端需要一个MVC框架或者可以提供帮助 许多。 我使用过服务器端的PHP框架。</ p>

我很困惑,我对JavascriptMVC项目的理解是他们处理浏览器捕获事件的客户端事件,执行AJAX请求,管理 来自服务器的响应/数据,并在图形界面中向用户显示它们。 </ p>

我知道在PHP MVC项目中我们还有控制器(和操作),它们中的任何一个都是一个带有单个入口点的单独页面,我的观点是这些页面是整个HTTP请求 。</ p>

我认为这两个框架的组合将采用单个</ strong>或少数</ em>重文件(包括js, css,imgs等)由另一个Javascript库加载和管理,例如steal.js。

NNow用户可以使用网站及其操作(作为事件)导致运行可能在UI中更改某些内容或导致AJAX请求的js函数,如在Yahoo Mail中,其中大多数事情发生在 one </ em>中 页面。</ p>

那么这将如何影响PHP中控制器和操作的设计? 我的意思是通常在PHP MVC框架中,很多控制器和动作意味着很多页面。 我认为,由于AJAX,控制器和动作的数量实际上应该更少。 我还认为,由于JMVC,大多数控制器(和操作)应该转向AJAX响应器,但是在这种情况下如何处理布局和视图?</ p>

最后</ p>
\ n


  • 我想知道使用此方法的不同方面(JMVC + MVC)。 (我使用Yii作为我的服务器端MVC框架,使用JavascriptMVC作为我的客户端MVC)。 </ li>
  • 我也想知道客户端的数据管理。</ li>
  • 我想了解如何使用AJAX和网络套接字,我们在哪里 可以使用AJAX和我们可以使用websockets的地方吗? </ li>
  • 我想了解本地存储如何将其用于模拟页面数据管理和缓存,我们如何以页面形式缓存来自服务器的数据作为JSON? 我正在开展一个非常大的项目,我想建立自己的基础非常强大。</ li>
    </ ul>
    </ div>

展开原文

原文

I have asked this quesion few days before here and no one answered it.
I had asked it in forum.javascriptMVC.com too and now I have a answer, however I need a a bit more idea.

Question:

I read javascriptMVC's documents and I loved it. 
But I don't know how to use it in a large scale project.

I think on the server-side a MVC framework is needed or can help so much. And I've worked with server side PHP frameworks.

I am confused, my understanding of JavascriptMVC projects is that they handle client side events on the browser capture events, execute AJAX requests, manage the responses/data from the server and also show them to user in a graphical interface.

I know that in PHP MVC projects we also have controllers (and actions) that any of them is a separate page with a single entry point, my point is that these pages are whole HTTP requests.

I think the combination of these two frameworks would be in a form of a single or few heavy files (including js , css , imgs etc) that loads and managed by another Javascript libary such as steal.js. Now user can work with site and its actions (as events) that result in running js functions that may change something in the UI or cause a AJAX request, as in Yahoo Mail where most things happens in one page.

So how will this affect the design of controllers and actions in PHP ? I mean normally in PHP MVC frameworks a lot of controllers and actions means a lot of pages. I think because of AJAX the number of controllers and actions should be actually less. I also think because of JMVC most controllers (and actions) should turn to AJAX responders, however how are layouts and views to be handled in this context?

Finally

  • I want to know about different aspects of using this method(JMVC+MVC). (I am using Yii as my server-side MVC framework and JavascriptMVC as my client-side MVC).
  • I also want to know about management of data on the client-side.
  • I would like to understand how AJAX and web-sockets could be used, where we can use AJAX and where we can use websockets?.
  • I want to understand about local-storage how we can use it for simulated page data management and maybe caching, how we can cache data coming from server as JSON in a form of a page? I am working on a very large project and I want to build its foundation very strong.

dongzhong5573
dongzhong5573 要回答关于何时使用AJAX(REST)以及何时使用websockets的问题,它实际上取决于您的应用程序需求,您是否需要来自服务器的实时(最多第二个)数据,这些数据会发生无法预测的变化?根据您的应用程序(您需要数据的全双工连接吗?)和用户的浏览器(目前只有Chrome支持它),您可能会发现使用websockets更有优势。(您可以阅读有关websockets的更多信息)本地存储可用于非敏感数据,这些数据不会经常从每个用户的访问中更改。例如。档案设置
7 年多之前 回复
dsunj08246
dsunj08246 是的更多细节plz。我想知道这个方法中的不同的东西(JMVC+MVC)。我使用yii作为服务器端框架和javascriptMVC作为客户端mvc。而且我想知道客户端的管理数据。以及如何使用AJAX和websockets(我的意思是我们可以在哪里使用AJAX以及我们可以使用websockets的地方?)。或者关于本地存储我们如何将它用于模拟页面数据管理和追逐。我的意思是我们如何以页面的形式将服务器中的数据缓存作为JSON缓存?我正在开展一个非常大的项目,我想建立一个非常强大的基础。非常感谢。
7 年多之前 回复
duanbi7247
duanbi7247 如果您想要更多细节,您必须说出您想要的更多细节。
7 年多之前 回复
drtppp75155
drtppp75155 好的,我参加了:)
7 年多之前 回复
douzong6649
douzong6649 然而,我想要回答这个问题,是否有任何javascriptMVC框架设计用于服务器端后端而不是试图完全替换服务器端框架。我看过Ember.js,在他们的例子中,整个应用程序都是在客户端构建的。
7 年多之前 回复
duangu9997
duangu9997 正常情况下,当服务器端控制器被http请求路由到时,它将以视图的形式返回响应(html标记)。如果http请求是AJAX请求,则服务器端控制器的响应将返回到AJAX回调。通过这种方式,javascript可以通过URI从后端检索数据。这是我目前使用Kohana框架并使用jQuery生成http请求的方法。
7 年多之前 回复
dongshuo6503
dongshuo6503 在我看来,javascript框架不提供使用服务器端框架的示例。
7 年多之前 回复
douya1974
douya1974 和来自forum.javascript.com的alex说这个。1)通常使用JMVC构建单页应用程序(尽管您可能有多个页面用于具有不同布局的复杂应用程序并在其间共享加载的代码)2)使用JMVC,您将使用模板在客户端呈现所有视图(EJS,Mustache)3)在服务器上,控制器的动作应该实现JSONAPI(RESTful或类似)-所以你的服务器用数据而不是html响应。如果任何机构有其他想法,你将使用客户端逻辑(模型,视图,控件)向用户表示这些数据请帮我。
7 年多之前 回复

3个回答

Say that you jave a JMVC framework where

  • The model gets data from the server using AJAX request - expecting JSON results.
  • The view does not rely on the server, for more that providing the raw HTML.
  • The Controllers do not rely on the server, for more that serving the JS files.

Essentially you use the server what it "should" be used for, data storage and processing, while you let your client browser handle all the tedious stuff.

Now, lets see how to define a server-side framework. As I see it we have several options, all of them fairly similar, albeit somewhat different (all returning someing in JSON format):

  • A fully fledged MVC such as cakePHP
  • Custom implementation
  • WebService implementation

Personally I would use a WebService, and I already have. Or rather, I used a WebSocket based JSON-RPC WebService. Using a fully fledged MVC will have it's drawbacks in maintainability and, not insignificantly, server load. But it is very possible, just implement a view which formats the page as JSON...

Making a JMVC client does not, in my view, mean that it cannot request new HTML from the server. But it does mean that that requested HTML should be free of data, other than meta-data the Java-View needs to know where to put data recieved from, for example, the WebService.

So a main page in the JMVC could just contain a single

<div id=content></div>

and menu clicks can fetch a subpage from the server and load the content into content. Then that loaded content can contain some more javascript which starts WebService requests to get data from the server, to display in the empty placefolders it in turn contains.

First check https://stackoverflow.com/a/4458566/718224 after that you can move forward.


Try this (from https://stackoverflow.com/a/8424768/718224)

No, you don't need to use it server-side, but it will help with organization / separation of application and business logic. Depending on the scale of your application, that could help tremendously in the future.

The key is just making sure you organize your backend code well, otherwise you will end up with a monolithic and/or difficult-to-maintain codebase.

Server-side views would contain your HTML and any JavaScript that may or may not make requests to the server. This assumes that you are actually using PHP to build the pages that a user navigates to.

If you have a static html page that builds itself using AJAX requests, then you may not need to use server-side views at all. Your controllers would more than likely be outputting JSON data. If this is the case, it doesn't make models and controllers any less useful.


Try this (from https://stackoverflow.com/a/8424760/718224)

If you are using any of the major PHP frameworks (CakePHP, Code Igniter, Symfony, etc.) then you ARE using MVC already. If your server side logic is more complex than just a few really simple scripts than you probably should be using one of those frameworks listed, using MVC on the server and the client.

Many (most?) larger web apps being built today are moving towards using an MVC framework for both client-side and server-side application code. It's a fantastic pattern for separating concerns for many large applications, especially request/response server apps and event-driven browser apps.


Try this (from https://stackoverflow.com/a/8427063/718224)

Backbone.js connects your application over a RESTful JSON interface. I honestly find that it works wonderfully in conjunction with the MVC framework. If you build a RESTful API, you can let you server manage CRUD updates quite easily. All your server side code will be responsible is saving and sending back JSON objects to Backbone.js. Then let most of your logic and magic happen within the Backbone.js framework.


Try this (from https://stackoverflow.com/a/8078282/718224)

First, a client-side MVC framework like Backbone isn't just for single-paged apps. You can also use it to add some rich interaction to one or many views of a more traditional app. They simply provide structure and data abstractions on the client.

Next, these client-side frameworks are designed specifically to work with your back-end MVC frameworks. Backbone.js (since you tagged it specifically) models and collections work with REST services. They will talk via GET/POST/PUT/DELETE verbs and will ultimately communicate with your controllers on the back-end when they make asynchronous requests.

In the case of Backbone, it talks JSON instead of HTML. In the case of Rails, this is really easily handled in the controller. If the request is an HTML one, then you return a view as HTML. If it is a JSON request (*.json or Content-type) then the controller returns a JSON representation of the data. I am assuming that it is as easy in Django as it is in Rails to have the same controller respond to multiple content requests (HTML, XML, JSON, etc)

may this help you.

douwen9534
douwen9534 好的,我理解,谢谢。 过了一段时间我改变了它。
7 年多之前 回复
dsiimyoc804955
dsiimyoc804955 Advait,可以参考其他答案,但不能直接复制粘贴它们而不归因。 前三部分只是这个问题答案的副本,最后一部分是这个答案的副本。 如果您从其他答案或其他网站复制文本,请提供原始链接,并使您更清楚自己没有写文本。
7 年多之前 回复



客户端Web应用程序和富客户端网页通常使用 jmvc </ code> backbones </ code> 等等,如果js库和 HTML5 </ code>技术(如 webstorage </ code>),您可以拥有更多类似网站的应用程序,这些应用程序可以在客户端进行,例如模板管理等。 我们只有ajax请求/响应服务器来获取/设置数据或更新状态。 和abput第一部分他们是对的jmvc网站更像是单页网站。 即hotmail雅虎等</ p>
</ div>

展开原文

原文

client side web apps and rich client side web pages often use jmvc backbones and etc. and with that kind if js libraries and HTML5 technologies like webstorage you can have a more applocation like websites that all things happens in client side like templating management and etc. and just we have ajax request/response to servers to get/set data or update status. and abput first section they are right a jmvc site are more like a single pages websites. ie hotmail yahoo , etc.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐