将新的内部对象插入到对象数组observable数组中并更新DOM

时间:2016-05-13 08:35:37

标签: javascript html knockout.js

带有绑定的HTML:

<div class="container" data-bind="foreach: { data: Conferences, as: 'conf' }">
    <div class="conf" data-bind="attr: { id : conf.Id }">
        <div class="conf-month" data-bind="text: conf.StartTime"></div>
        <div class="conf-day" data-bind="text: conf.EndTime"></div>
        <div class="add-user">
            <input type="tel" data-bind="value: $root.PhoneNumber />
            <input type="email" data-bind="value: $root.Email" />
            <a id="add-user" title="Add new user" data-bind="attr: { value: conf.Id }, click: $root.addUserToConference">Add</a>
        </div>
        <div class="conf-users" data-bind="foreach: { data: conf.ConferenceUsers, as: 'user' }">
            <div class="conf-user" data-bind="attr: { id: 'confuser-' + user.Id}">
                <span data-bind="text: user.Name"></span>
                <span data-bind="text: user.PhoneNumber"></span>
                <span data-bind="text: user.Email"></span>
            </div>
        </div>
    </div>
</div>

KnockoutJs ViewModel:

function ConferenceViewModel() {
    var self = this;

    self.Conferences = ko.observableArray([]);
    self.PhoneNumber = ko.observable("");
    self.Email = ko.observable("");


self.getAllConfs = function () {
        $.ajax({
            type: 'GET',
            url: '/confs/getconfs',
        }).done(function (confs) {
            $.each(confs, function (index, conf) {
                    //populate the list on document ready
                    confVm.Conferences.push(conf);
            };
        }).fail(showError);
}

self.addUserToConference  = function (viewModel, event) {
    var user = {
        "Id": 0,
        "ConferenceId": viewModel.Id(),
        "Email": "self.Email()",
        "PhoneNumber": self.PhoneNumber(),
    };  

    // here we should insert a new ConferenceUser into the Conferences observable array
    // and also update the DOM 

    // ajax to insert user to db
}

通过ajax填充上面的会议observableArray后, console.log(ko.toJSON(confVm.Conferences()))的输出如下:

[  
   {  
      "ConferenceUsers":[  
         {  
            "Id":3006,
            "ConferenceId":8,
            "Name":null,
            "Email":"mail@lala.com",
            "UserName":null,
            "PhoneNumber":"234234234234",
            "ChannelId":null,
            "State":null,
            "Type":null,
            "RecordingId":null
         }
      ],
      "Id":8,
      "Subject":"YYYhaaaaa",
      "StartTime":"2016-05-29T18:30:00",
      "EndTime":"2016-05-29T19:30:00",
      "OrganizerEmail":"elpas@live.com",
      "OrganizerName":"dasdasd",
      "Pin":"6402",
      "BridgeId":null,
      "State":null
   },
   {  
      "ConferenceUsers":[  
         {  
            "Id":3013,
            "ConferenceId":12,
            "Name":null,
            "Email":"dsfdfsdfdsf@dfdfdf.com",
            "UserName":null,
            "PhoneNumber":null,
            "ChannelId":null,
            "State":null,
            "Type":null,
            "RecordingId":null
         }
      ],
      "Id":12,
      "Subject":"dsfsdfdsfsdf",
      "StartTime":"2016-05-31T22:00:00",
      "EndTime":"2016-05-31T23:00:00",
      "OrganizerEmail":"d@adssad.com",
      "OrganizerName":"dsfdsfsdf",
      "Pin":"3402",
      "BridgeId":null,
      "State":null
   }
]

问:如何通过ConferenceId插入新的ConferenceUser并相应地更新DOM?

2 个答案:

答案 0 :(得分:1)

试试这个:

var obj = [  
   {  
      "ConferenceUsers":[  
         {  
            "Id":3006,
            "ConferenceId":8,
            "Name":null,
            "Email":"mail@lala.com",
            "UserName":null,
            "PhoneNumber":"234234234234",
            "ChannelId":null,
            "State":null,
            "Type":null,
            "RecordingId":null
         }
      ],
      "Id":8,
      "Subject":"YYYhaaaaa",
      "StartTime":"2016-05-29T18:30:00",
      "EndTime":"2016-05-29T19:30:00",
      "OrganizerEmail":"elpas@live.com",
      "OrganizerName":"dasdasd",
      "Pin":"6402",
      "BridgeId":null,
      "State":null
   },
   {  
      "ConferenceUsers":[  
         {  
            "Id":3013,
            "ConferenceId":12,
            "Name":null,
            "Email":"dsfdfsdfdsf@dfdfdf.com",
            "UserName":null,
            "PhoneNumber":null,
            "ChannelId":null,
            "State":null,
            "Type":null,
            "RecordingId":null
         }
      ],
      "Id":12,
      "Subject":"dsfsdfdsfsdf",
      "StartTime":"2016-05-31T22:00:00",
      "EndTime":"2016-05-31T23:00:00",
      "OrganizerEmail":"d@adssad.com",
      "OrganizerName":"dsfdsfsdf",
      "Pin":"3402",
      "BridgeId":null,
      "State":null
   }
];


/* Iterate all conferences */
for (var i in obj) {

  /* Find conference with ID = 8 */
  if (obj[i].Id === 8) {

    /* Add a new user to the conference */
    obj[i].ConferenceUsers.push({

      "Id":1111,
      "ConferenceId":1,
      "Name":null,
      "Email":"test@example.com",
      "UserName":null,
      "PhoneNumber":null,
      "ChannelId":null,
      "State":null,
      "Type":null,
      "RecordingId":null

    });

    break;
  }
}

console.log(obj); // output result

答案 1 :(得分:1)

您需要执行以下四个步骤:

  1. 从可观察数组Conferences
  2. 获取当前的会议列表
  3. 找到ID正确的会议
  4. 将新用户推送到此会议ConferenceUsers
  5. 确保使用新数据设置Conferences
  6. 虽然所有步骤都非常直接执行,但它们的工作方式会有一些缺点:

    Conference个对象和ConferenceUsers数组无法观察到。 Knockout无法自动识别{strong>内部 Conference个对象的任何更改。因此,在第3步和第4步之后,为了淘汰,它似乎不会出现任何变化:Conferences数组中仍有相同的对象。< / p>

    我的建议:

    如果将新用户添加到会议中会定期发生,我建议您创建一个Conference视图模型,其中包含ko.observableArray个用户。或者,您可以为每个次要更改创建 new Conference对象,这将触发knockout重新呈现整个UI而不仅仅是相关部分(假设您已使用过foreach数据绑定在某处。)

    如何将常规Conference对象映射到视图模型的快速示例:

    &#13;
    &#13;
    // Your JSON data from the ajax request (an array of objects)
    var conferencesData = [];
    
    
    var Conference = function(conferenceJSON) {
    
      // It's better to map all properties you're using individually, 
      // but this at least exposes the original JSON object
      this.props = conferenceJSON;
    
      this.users = ko.observableArray(conferenceJSON.conferenceUsers);
    
    };
    
    var createConference = function(conferenceJSON) {
      return new Conference(conferenceJSON);
    };
    
    var ConferenceList = function(conferencesJSON) {
      var self = this;
    
      this.conferences = ko.observableArray(conferencesJSON.map(createConference));
    
      this.addConference = function(conferenceJSON) {
        self.conferences.push(createConference(conferenceJSON));
      };
    
      this.addUserToConference = function(userJSON) {
        var conferences = self.conferences();
    
        for (var c = 0; c < conferences.length; c += 1) {
          // Step 2: Find the conference with the required id
          if (conferences[c].props.id === userJSON.ConferenceId) {
            // Step 3: We're pushing to an observableArray, so no need
            // to worry about Step 4.
            conferences[c].users.push(userJSON);
            return true;
          }
        }
    
        return false;
      };
    
    };
    
    ko.applyBindings(new ConferenceList(conferencesData));
    &#13;
    &#13;
    &#13;