MVC与Flux?双向与单向?

时间:2015-10-31 03:20:01

标签: model-view-controller reactjs flux

查看下图(解释MVC),我看到了单向数据流。

那么为什么我们认为MVC在证明Flux的同时具有双向数据流?

MVC Pattern

4 个答案:

答案 0 :(得分:28)

Real和Pure MVC是单向的。从问题中粘贴的维基百科图中可以清楚地看出。

十多年前,当像Apache Struts这样的服务器端框架实现了一种名为Model View Presenter(MVP)模式的MVC变体时,他们使每个请求都通过控制器,每个响应都通过控制器返回。每个人都继续称它为MVC。由于Web的固有特性,如果没有发送请求或更新的视图,则无法将模型中的任何更改传播到视图。所以Pure MVC没有实现。而是实施MVP。

几年前,当Angular,Ember,Knockout等框架在前端实现MVC时,他们实现了另一种名为Model View ViewModel(MVVM)模式的MVC变种,很少有人继续称之为MVC。 (很少有人意识到术语并不重要,称之为MVW(W代表Whatever)),它们都没有实现纯MVC。

当React诞生时,他们抓住机会实现纯MVC(不是MVP或MVVM),并将其重命名为Flux,几乎没有变化。我觉得Flux是MVC的另一个变种。虽然,Flux / React团队表示它不是MVC,但我看到两种体系结构之间存在很多奇偶性 - Flux和MVC。

答案 1 :(得分:15)

因为在Javascript框架中,MVC不像你描述的那样工作。 UI通常与模型双向通信,如:

  1. 用户输入到视图输入
  2. MVC框架绑定onchange()以更新模型。
  3. Ajax请求带来了新的模型数据。
  4. MVC框架更新查看输入的值以匹配模型。
  5. 在Flux架构中,UI只会向调度程序发出一个带有类型和相关数据的独立操作,然后调度程序将以与任何后台ajax方法更新模型相同的方式更新模型。

    参考: http://www.thesoftwaresimpleton.com/blog/2013/03/23/client-side-mvc/

      

    "客户端MVC与服务器端MVC"

    完全不同      

    "我们正在两个对象之间建立双向通信......"

         

    "简而言之,我们将firstName属性的值连接在一起   Person对象的值属性输入。"

    http://guides.emberjs.com/v1.10.0/object-model/bindings/

      Ember.js中的

    绑定可以与任何对象一起使用,不仅仅是介于两者之间   观点和模型

答案 2 :(得分:15)

我是一名嵌入式开发人员,我在我的应用程序中使用MVC模式。我的应用程序非常小,我已经将我的架构设置为几乎单向的MVC。但是,我读了这篇文章,解释了客户端MVC,以及一些关于MVC和FLUX之间差异的想法。

参考: {{3}}

传统MVC

Private OutApp As Outlook.Application

Private Function getMailMessage(ByVal FileName As String) As String

    Dim outMsg As Object
    Dim outDraftFolder As MAPIFolder
    If Dir$(FileName) = "" Then
        'nothing to read
        getMailMessage = "File " & FileName & " not found"
        Exit Function
    End If
    If OutApp Is Nothing Then
        Set OutApp = New Outlook.Application
    End If

    Dim outFold As Outlook.Folders
    'get Draft folder of outlook
    Set outDraftFolder = OutApp.GetNamespace("MAPI").GetDefaultFolder(olFolderDrafts)
    'load message as draft - it may be something else than a mailitem...
    Set outMsg = OutApp.CreateItemFromTemplate(FileName)
    'check the type:
    Dim sText As String
    If TypeOf outMsg Is Outlook.MailItem Then
        With outMsg
            sText = "A mailItem:"
            sText = sText & vbCrLf & "sender =" & .SenderName
            sText = sText & vbCrLf & "Received = " & .ReceivedTime
            sText = sText & vbCrLf & "Created = " & .CreationTime
            sText = sText & vbCrLf & "subject = " & .Subject
            sText = sText & vbCrLf & "Body:" & vbCrLf
            sText = sText & vbCrLf & .Body
         End With
    ElseIf TypeOf outMsg Is Outlook.ContactItem Then

         With outMsg
            sText = "A ContactItem:"
            sText = sText & vbCrLf & "Created = " & .CreationTime
            sText = sText & vbCrLf & "subject = " & .Subject
            sText = sText & vbCrLf & "NickName=" & .NickName
            sText = sText & vbCrLf & "Email: " & .Email1Address
            sText = sText & vbCrLf & "Company Name: " & .CompanyName
            sText = sText & vbCrLf & "Profession: " & .Profession
            sText = sText & vbCrLf
            sText = sText & vbCrLf & "Body:" & vbCrLf
            sText = sText & vbCrLf & .Body
         End With
    ElseIf TypeOf outMsg Is Outlook.AppointmentItem Then

        With outMsg
            sText = "An AppointmentItem:"
            sText = sText & vbCrLf & "Created = " & .CreationTime
            sText = sText & vbCrLf & "subject = " & .Subject
            sText = sText & vbCrLf & "Conversation Topic=" & .ConversationTopic
            sText = sText & vbCrLf & "Importance: " & .Importance
            sText = sText & vbCrLf & "Duration: " & .Duration
            sText = sText & vbCrLf & "Last Modification time: " & .LastModificationTime
            sText = sText & vbCrLf
            sText = sText & vbCrLf & "Body:" & vbCrLf
            sText = sText & vbCrLf & .Body
        End With

    ElseIf TypeOf outMsg Is Outlook.MeetingItem Then
        Dim mx As Outlook.MeetingItem
        With mx
            sText = "A MeetingItem:"
            sText = sText & vbCrLf & "Created = " & .CreationTime
            sText = sText & vbCrLf & "subject = " & .Subject
            sText = sText & vbCrLf & "Conversation Topic=" & .ConversationTopic
            sText = sText & vbCrLf & "Importance: " & .Importance
            sText = sText & vbCrLf & "Expiry Time: " & .ExpiryTime
            sText = sText & vbCrLf & "Last Modification time: " & .LastModificationTime
            sText = sText & vbCrLf
            sText = sText & vbCrLf & "Body:" & vbCrLf
            sText = sText & vbCrLf & .Body
        End With
    Else
        sText = "You need to write a bit more of code..."
    End If
    getMailMessage = sText
    Set outMsg = Nothing
    Set outDraftFolder = Nothing

End Function

FLUX

|------|  request   |------------|  request   |-------|
|      | ---------> |            | ---------> |       |
| VIEW |  response  |            |  response  |       |
|      | <--------- |            | <--------- |       |
|------|            |            |            |       |
                    | CONTROLLER |            | MODEL |
|------|  request   |            |  request   |       |
|      | ---------> |            | ---------> |       |
| VIEW |  response  |            |  response  |       |
|      | <--------- |            | <--------- |       |
|------|            |------------|            |-------|

答案 3 :(得分:3)

有些人采用术语MVC来引用others had pointed out were not pure MVC但是可以称为MVP(Backbone),MVVM(Angular 1)或更广泛的变体的JavaScript框架MV *(另见Arun's answer)。

Facebook introduced Flux时,他们compared it to the issues with MVVM/MVP/MV*,但却混淆地使用了MVC一词。

对于观看此视频的纯MVC开发者来说,Facebook声明的MVC问题没有意义,Facebook对Flux的描述比他们描述的MVVM系统更接近MVC:

  

核心问题是他们“做错了”MVC。然后他们修复了它,但决定重新命名并说他们发明了解耦数据,视图和事件处理的模式

YouTube comment

  

看起来你的程序员创建了flux,因为他们不知道如何正确使用MVC和事件调度程序。

YouTube comment