我試圖更新文檔。具體來說,我正在改變項目的順序 - 一組對象。該數組被稱爲'資源'。我調用一個方法來更新資源數組中每個對象的位置。從查看數據庫中可以看出,文檔已按照正確的順序更新。流星彙集更新沒有正確更新客戶端
在更新時,如預期的那樣,客戶端反應 - 並用新數據更新 - 但是,它似乎是以錯誤的順序顯示數組......它就像它的「一出」一樣......(所以移動到索引2的項目出現在索引1 ...移動到索引3的項目出現在索引2等)但是當我做瀏覽器的物理刷新時 - 數據按預期加載。有人在這裏有類似(但不完全相同)的問題 - Meteor collection insert not updating on client - 雖然沒有任何答案對我的問題有所幫助。
在我router.js文件(我使用ironrouter)
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'notFound',
waitOn: function() {
return [ Meteor.subscribe('playlists'), Meteor.subscribe('resources') ];
}
});
.....
Router.route('/playlist/:_id', {
name: 'playlistPage',
data: function() {
return Playlists.findOne(this.params._id);
}
});
在客戶端,我有:
...
'click #saveOrder': function(e, template) {
e.preventDefault();
var currentplaylistId = (template.data._id);
var resourcesArray = [];
$('#playlistTableBody tr').each(function(i) {
var $this = $(this);
var data = {
title: $this.data('title'),
type: $this.data('type'),
resourceid: $this.data('resourceId'),
duration: $this.data('duration'),
order: i
}
resourcesArray.push(data);
});
Meteor.call('playlistUpdateResourceOrder',
currentplaylistId, resourcesArray, function(error, result) {
// display the error to the user and abort
if (error)
return throwError(error.reason);
// Refresh data?
Router.go('playlistPage', {_id: result._id});
});
和服務器
playlistUpdateResourceOrder: function (currentplaylistId, resourcesArray) {
Playlists.update(currentplaylistId, {
$set: { resources: resourcesArray }
});
return {
_id: currentplaylistId // return to our updated/renamed playlist
};
}
我的模板非常簡單:
<tbody id="playlistTableBody">
{{#each resources }}
{{>resourceItem}}
{{/each}}
</tbody>
和
<template name="resourceItem">
<tr data-title="{{title}}" data-type="{{type}}" data-resourceid="{{resourceId}}" data-duration="{{duration}}">
<td>{{title}}</td>
<td>{{type}}</td>
</tr>
</template>
謝謝托馬斯 - 但是,這是一個子集合(對象數組)而不是集合本身。我有一個文檔,其中包含一個名爲resources包含對象的數組。它的這個資源數組,我想更新。 – Rob
那麼什麼是Meteor.subscribe('資源')呢?對於排序子文檔,您可以使用Underscore sortBy函數:http://underscorejs.org/#sortBy –
資源集合是別的東西 - 對不起,我應該評論一下。我沒有得到的是 - 在我的tbody中,資源陣列以錯誤的順序寫出來。但是,如果我在頁面上放置相同的代碼,則會以正確的順序呈現。它必須與插件(影響tbody)有關。我刪除了插件並嘗試在Chrome調試中手動操作dom(從頂部切下第一個TR,並在底部粘貼)。然後我打了保存 - 並得到同樣的問題......所以它似乎是每當dom被手工操縱jquery或bruce force? – Rob