从输入向模型添加数据

Add data to model from input

我想知道如何从我的输入中向模式添加数据。 这是我的代码:

主文件

var Bintime = new Marionette.Application();

Bintime.on("before:start", function(){
    var RegionContainer = Marionette.LayoutView.extend({
        el: "#app",
        regions: {
            contacts: "#contacts",
            name: "#name",
            messages: "#messages",
            input: "#input"
        }
    });

    Bintime.regions = new RegionContainer();
});

Bintime.on("start", function(){
    Bintime.Contacts.List.Controller.listContacts();
});

型号

Bintime.module("Contacts", function(Contacts, Bintime, Backbone, Marionette, $, _){
    Contacts.Contacts = Backbone.Model.extend({});

    Contacts.ContactsCollection = Backbone.Collection.extend({
        model: Contacts.Contacts,
        comparator: "firstName"
    });

    var contacts;

    var initializeContacts = function(){
        contacts = new Contacts.ContactsCollection([
            {
                id: 1,
                firstName: "Alice",
                lastName: "Arten",
                messages: [
                    {
                        from: "1",
                        message: "Hello"
                    },
                    {
                        from: "1",
                        message: "How r u?"
                    },
                    {
                        to: "1",
                        message: "Hey!"
                    },
                    {
                        to: "1",
                        message: "Great!"
                    }
                ]
            },
            {
                id: 2,
                firstName: "Bob",
                lastName: "Brigham"
            },
            {
                id: 3,
                firstName: "Charlie",
                lastName: "Campbell"
            }
        ]);
    };

    var API = {
        getContactContact: function(){
            if(contacts === undefined){
                initializeContacts();
            }
            return contacts;
        }
    };

    Bintime.reqres.setHandler("contacts:contacts", function(){
        return API.getContactContact();
    });
});

主控制器

Bintime.module("Contacts.List", function(List, Bintime, Backbone, Marionette, $, _) {
    List.Controller = {
        listContacts: function() {
            var contacts = Bintime.request("contacts:contacts");

            var contactsView = new List.Contacts({
                collection: contacts
            });

            contactsView.on("childview:contact:contacts", function(childView, model) {
                Bintime.Contact.Name.Controller.nameContact(model);
            });

            contactsView.on("childview:chat:contacts", function(childView, model) {
                Bintime.Contacts.Messages.Controller.chatMessages(model);
            });

            contactsView.on("childview:message:contacts", function(childView, model) {
                Bintime.Message.Input.Controller.messageField(model);
            });

            Bintime.regions.contacts.show(contactsView);
        }
    }
});

模块控制器,添加来自输入的消息

Bintime.module("Message.Input", function(Input, Bintime, Backbone, Marionette, $, _) {
    Input.Controller = {
        messageField: function(model){
            var contacts = new Input.Contacts({
                model: model
            });

            Bintime.regions.input.show(contacts);
        }
    }
});

查看模块,添加来自输入的消息

Bintime.module("Message.Input", function(Input, Bintime, Backbone, Marionette, $, _) {
    Input.Contacts = Marionette.ItemView.extend ({
        template: "#new",
        events: {
            "change input#message-input": "sendInput"
        },
        sendInput: function(e){
            e.preventDefault();
            var message = this.$("input#message-input").val();
            console.log(message);
        }
    });

    $('#new').append(v.el);
});

index.html

<div id="app" class="full-width full-height">
      <div class="full-width full-height">
        <div class="large-3 column contacts nopadding full-height">
          <div id="contacts">
          </div>
        </div>
        <div class="large-9 column full-height nopadding">
          <div id="name" class="user full-width">
            <div>

            </div>
          </div>
          <div id="messages">
          </div>
          <div id="input">
          </div>
        </div>
      </div>
    </div>
    <script type="text/template" id="contacts-list">
      <div class="contact">
        <div class="center text-center">
          <a href="#"><span class="text-center"><%= firstName%> <%= lastName%></span></a>
        </div>
      </div>
    </script>
    <script type="text/template" id="user">
        <h5 class="text-center"><%= firstName%> <%= lastName%></h5>
    </script>
    <script type="text/template" id="message">
      <% _.each(messages, function (item) { %>
        <div class="message">
          <div>
            <% if (item.from) { %>
              <div class="in">
                <%= item.message %>
              </div>
            <% } else if (item.to) { %>
              <div class="out">
                <%= item.message %>
              </div>
            <% } %>
          </div>
        </div>
      <% }); %>
    </script>
    <script type="text/template" id="new">
      <div class="enter full-width">
        <input id="message-input" type="text" placeholder="Enter your message..">
      </div>
      <a href="#" id="send" class="button">
        <span>Send</span>
      </a>
    </script>

如您所见,我已经从输入字段中获取数据,此处:

sendInput: function(e){
   e.preventDefault();
   var message = this.$("input#message-input").val();
   console.log(message);
}

但是我无法将我的数据添加到模型中。我试着用 .set() 来做,但它覆盖了我所有的 messages 数组。类似的东西,但不完全相同:

sendInput: function(e){
    e.preventDefault();
    var field = $(e.currentTarget);
    var message = this.$("input#message-input").val();
    var data = {};
    data[field.attr("messages")] = message;
    this.model.set(data);
}

将“111”字符串添加到模型的结果,以前面的示例为例:

所以,基本上我需要向我的模型添加新消息。像这样:

    id: 1,
    firstName: "Alice",
    lastName: "Arten",
    messages: [
        {
            from: "1",
            message: "Hello"
        },
        {
            from: "1",
            message: "How r u?"
        },
        {
            to: "1",
            message: "Hey!"
        },
        {
            to: "1",
            message: "Great!"
        },
        {
            to: "1",
            message: "HERE SHOULD BE NEW MESSAGE"
        }
   ]

假设您可以通过 this.model 访问模型,其属性散列如下所示:

{
   id: 1,
   firstName: "Alice",
   lastName: "Arten",
   messages: [{
        from: "1",
        message: "Hello"
      },
      {
        from: "1",
         message: "How r u?"
      },
      {
         to: "1",
         message: "Hey!"
      },
      {
         to: "1",
         message: "Great!"
      }]
}

如问题所示,您可以像这样向其中添加一条新消息:

this.model.get("messages").push({
    to: "1", // fill this as per your logic
    message: message 
});

之所以可行,是因为在 JavaScript 中对象是通过引用传递的,而数组是特殊对象。