所以我有一個可以正常工作的observablearray,但UI不更新。我讀過很多人遇到過這類問題,但我沒有看到它。Knockout ObservableArray不更新HTML Foreach
因此,HTML是
<tbody data-bind="foreach: tweets">
<tr>
<td>
<span data-bind="visible: created_at" class="label label-success">Yup</span>
</td>
<td><p><b data-bind="text: screen_name"></b></p></td>
<td><p><b data-bind="text: id"></b></p></td>
<td><p data-bind="text: text"></p></td>
<td><p data-bind="text: created_at"></p></td>
</tr>
</tbody>
和JavaScript是調用API,並從它建立一個數組的函數。
<script type="text/javascript">
function TweetsViewModel() {
var self = this;
self.tasksURI = 'http://localhost:8000/api/v1/tweet/';
self.tweets = ko.observableArray();
self.ajax = function(uri, method, data) {
var request = {
url: uri,
type: method,
contentType: "application/json",
Accept: "application/json",
cache: false,
dataType: 'json',
data: JSON.stringify(data)
};
return $.ajax(request);
}
self.ajax(self.tasksURI, 'GET').done(function(data) {
for (var i = 0; i < data.objects.length; i++) {
var tweet = this;
tweet.created_at = ko.observable(data.objects[i].created_at)
tweet.text = ko.observable(data.objects[i].text)
tweet.id = ko.observable(data.objects[i].id)
tweet.screen_name = ko.observable(data.objects[i].twitteruser.screen_name)
self.tweets.push(tweet)
}
});
setTimeout(TweetsViewModel, 10000)
}
ko.applyBindings(new TweetsViewModel());
</script>
僅用於測試目的,我使用setTimeout來重新運行對API的調用並更新數組。數組得到正確更新,但UI不。我爲自己的無知道歉(長期以來一直是後端開發者,這是我10年來首次使用Javascript)。任何幫助非常感謝!
謝謝。我認爲這可能是這樣的,但沒有看到它。這清除了它的權利。我真的很感謝你用時間棒擊敗我。 – user3314493