当用Ember-Data保存时,所有项目上的数据复制
Data duplicating on all items when saving with Ember-Data
我有一个页面,用户在其中编辑上传的照片,并使用Ember-Data在模型上为单个照片应用标记。但是,在保存到控制器上并过渡到列出所有照片的页面后,标签将出现在所有项目上,并取代之前存在的任何标记。如果我重新打开页面,标签根本没有保存。
我不太确定是什么导致了这个问题。如有任何帮助,不胜感激。
//The photo model
App.Photo = DS.Model.extend({
title: attr(),
description: attr(),
image: attr(),
width: attr(),
height: attr(),
important_top: attr(),
important_left: attr(),
important_bottom: attr(),
important_right: attr(),
created: attr('date'),
authors: hasMany('author'),
app_data: {
tags: []
},
imageURL: function() {
return document.location.origin + '/media/' + this.get('image');
}.property('image'),
});
// Photo edit route
App.PhotoseditRoute = Ember.Route.extend({
model: function() {
this.store.find('photo');
// Populate model with photos from the lowest upload ID to higest.
return this.store.filter('photo', function(image){
return image.get('id') >= App.Upload.uploadedImages[0]; // Get data from uploader
});
},
activate: function() {
$('#page-title').text('Edit Photos');
},
});
// Photo Edit Controller
App.PhotoseditController = Ember.ObjectController.extend({
parsedTags: function() {
// Get tags from the view's input field
var tagData = this.get('app_data').tags;
// Convert tags to an array
return tagData.join(',');
}.property('app_data'),
// Watch parsedTags and apply array to model when converted
parsedDataChanged: function() {
Ember.run(this, function() {
this.get('app_data').tags = this.get('parsedTags').split(',');
});
}.observes('parsedTags'),
actions: {
save: function() {
var that = this;
that.get('model').save().then(function(success) {
that.transitionToRoute('photos');
});
}
}
});
// Photo edit template
<h2>Edit Photo Meta Data</h2>
<button {{action 'save'}} style="float:right;">Save All</button>
<table>
<thead>
<tr>
<th></th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{{#each object in content itemController='photosedit'}}
<tr>
<td><img {{bind-attr src="imageURL"}} width="200" /></td>
<td>{{input title valueBinding="title"}}</td>
<td>{{input description valueBinding="description"}}</td>
<td>{{input parsedTags valueBinding="parsedTags"}}</td>
</tr>
{{else}}
<tr>
<td colspan="6">No photos yet.</td>
</tr>
{{/each}}
</tbody>
</table>
问题来自于您声明app_data
的方式。该变量将在App.Photo
的所有实例中共享,这解释了为什么您看到所有照片都获得相同的标签。
您可以通过以不同的方式初始化属性来解决这个问题:
App.Photo = DS.Model.extend({
init: function() {
this._super();
this.app_data = { tags: [] };
}
});
不是App.Photo = DS.Model.extend({
app_data = { tags: [] }
});
请参阅此JsBin以获得突出显示问题和解决方案的示例http://emberjs.jsbin.com/wawoq/3
当您调用save()并从那里进行跟踪时,您需要检查是否使用正确的数据调用了存储。
除此之外,parsedTags和parseddatachchanged似乎是相互引用的
相关文章:
- 正在将数据主题添加到所有项目
- 谷歌图表堆叠柱状图.如何设置每个单独堆叠项目(数据系列)的样式
- 如何处理数据视图中项目的鼠标点击
- 在Knockout JS中搜索从DB加载数据的项目
- Firebase-正在从推送项目中检索数据
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- Grunt angularJs项目没有来自后端的依赖性's数据
- 显示离子项目中json文件中的数据
- jQuery.数据集's或获取's的项目不对
- 在没有JQuery的情况下将JSON数据获取到TVML项目中
- PHP根据用户选择的项目以模式加载数据
- 筛选源数据后的项目工具提示
- 在FB.api函数中创建一个数据项目
- 我无法在谷歌图表的代码 javascript 中获取 json 数据(项目 Python Hello Dashboard
- 如何在选择项目时使用 ajax 将数据提取到输入字段中
- 如何遍历项目列表并将其数据值推送到数组中
- 尝试使用PHP / Javascript构建一个可点击的索引,该索引显示单击的每个项目的数据
- 如何移除 DOJO 数据存储添加到我提取的项目中的所有额外字段
- addClass/removeClass 基于所选项目数据属性
- 如果我的模型是MVC3中的QueryTable,如何访问Javascript中的项目数据