我正在Ember.js和Rails中構建一個Social Bookmarking站點。我正在使用ember-rails寶石。我無法破壞Ember.js一側的書籤記錄。我確認他們正在被服務器刪除,並返回200代碼。我有一個擁有許多主題的用戶模型,並且主題有許多書籤。以下是奇怪的事情:主題被銷燬,沒有問題。他們永遠不會再出現在模板中。Ember JS:在調用destroyRecord並創建新記錄後模型重新出現
但是,當書籤被刪除時,它們似乎消失了;但是,當創建新記錄時,書籤會再次出現並且無法再被銷燬。刷新瀏覽器時,重新出現的書籤消失。
下面是我的主題模板的代碼,從那裏的書籤可以被刪除:
{{#each bookmark in bookmarks}}
<div class="media">
<div class="media-left">
<img class="media-object" style="width:64px; height: 64px; margin-right: 20px; border-radius: 50%;" {{bind-attr src=bookmark.image}}><br>
</div>
<div class="media-body">
<h4 class="media-heading">
<a {{bind-attr href=bookmark.url}} }}>{{ bookmark.title }}</a></h4>
{{#if bookmark.isUpdating}}
<form style="display: inline;" {{ action 'updateBookmark' bookmark bookmark.url on='submit'}}>
<small>{{input placeholder=bookmark.url value=bookmark.url}}</small>
</form>
{{else}}
<small>{{ bookmark.url }}</small>
{{/if}}<br>
{{ bookmark.description }}
<div><hr>
{{#if bookmark.likedByCurrentUser}}
<button {{action 'destroyLike' bookmark bookmark.likes controllers.current_user.currentUser }} class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-thumbs-down" aria-hidden="true"></span> Unlike
</button>
{{else}}
<button {{action 'createLike' bookmark }} class="btn btn-primary" type="button">
<span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span> Like
</button>
{{/if}}
{{#if belongsToCurrentUser}}
{{#if bookmark.isUpdating}}
<button class="btn btn-default" {{action 'updateBookmark' bookmark bookmark.url }}><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Save</button>
{{else}}
<button class="btn btn-default" {{ action 'updateBookmarkToggleOn' bookmark }}><span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Update</button>
{{/if}}
<button class="btn btn-default" {{ action 'destroyBookmark' bookmark }}><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Delete</button>
{{/if}}
</div>
</div>
</div>
</div><br>
{{/each}}
這裏的TopicController
Blocmarks.TopicController = Ember.ObjectController.extend({
needs: ['current_user'],
bookmarks: (function() {
return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
sortProperties: ['title'],
content: this.get('content.bookmarks')
});
}).property('content.bookmarks'),
actions : {
destroyBookmark: function(bookmark) {
bookmark.destroyRecord();
},
createBookmark: function (topicId) {
var bookmark = this.store.createRecord('bookmark', { url: this.get('url'), topic: this.get('model') });
bookmark.save();
this.set('url', '');
},
updateBookmarkToggleOn: function(bookmark){
bookmark.set('isUpdating', true);
},
updateBookmark: function(bookmark, url){
bookmark.set('url', url);
bookmark.save();
bookmark.set('isUpdating', false);
},
destroyTopic: function(topic) {
topic.destroyRecord();
this.transitionToRoute('topics');
},
updateToggleOn: function(topic){
topic.set('isUpdating', true);
},
updateTopic: function(topic, title){
var controller = this;
topic.set('title', title);
topic.save();
topic.set('isUpdating', false);
},
createLike: function(bookmark){
controller = this;
if (bookmark.get('likedByCurrentUser') == true){
alert("Nope. You've already liked this once!");
} else {
this.store.find('bookmark', bookmark.id).then(function (bookmark) {
var like = controller.store.createRecord('like', {bookmark: bookmark, likedByCurrentUser: true});
like.save();
});
}
bookmark.set('likedByCurrentUser', true);
},
destroyLike: function(bookmark, likes, user){
this.store.find('like', {bookmark_id: bookmark.id, user_id: user.id}).then(function(likes){
likes.objectAtContent(0).destroyRecord();
});
bookmark.set('likedByCurrentUser', false);
},
sortByTitle: function(){
this.get('bookmarks').set('sortProperties', ['title']);
this.get('bookmarks').set('sortAscending', true);
$('#sort-by a').removeClass('active');
$('#sort-by-title').addClass('active');
},
sortByURL: function(){
this.get('bookmarks').set('sortProperties', ['url']);
this.get('bookmarks').set('sortAscending', true);
$('#sort-by a').removeClass('active');
$('#sort-by-url').addClass('active');
},
sortByCreated: function(){
this.get('bookmarks').set('sortProperties', ['created_at']);
this.get('bookmarks').set('sortAscending', false);
$('#sort-by a').removeClass('active');
$('#sort-by-created').addClass('active');
}
}
});
下面是該TopicRoute代碼:
Blocmarks.TopicRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('topic', params.topic_id);
}
});
提前感謝您提供的任何幫助。請讓我知道我是否可以提供有助於解決此問題的其他信息。
更新:我注意到,如果我檢查indexOf被銷燬的項目,它仍然存在於-1。這是正常的嗎?在Ember Inspector中,它顯示在數組的內容中,但似乎沒有反映在數組的長度中。
已解決:我的路線正在返回「ManyArray」;顯然這就是導致這個問題的原因,因爲我改變了獲取所有書籤的路由,然後在控制器級按主題過濾它們。這導致了一個「RecordArray」作爲書籤的模型。