我正在使用这个漂亮的 Ember.js 混合:https ://github.com/Wildhoney/EmberDroplet使用拖放上传图像,它工作得很好。
唯一的问题是,我不知道如何在上传图片后将它们添加到视图中。该模型在我的 Rails API 中得到正确更新,但是,由于没有真正刷新整个页面,我不知道如何在 Ember 中更新模型。
这是我的一些代码片段:
router.js.coffee
App.Router.map ->
@resource('site', { path: '/' }, ->
@resource('media')
)
routes.js.coffee
App.MediaRoute = Ember.Route.extend
model: (params)->
return App.Media.find()
media.handlebars
<div class="row">
<div id="media-container" class="large-12">
<div class="row">
{{#each media in model}}
<div class="large-4 columns">
<a class="th" {{bind-attr href='media.url'}}>
<img {{bind-attr src='media.thumb'}}>
</a>
</div>
{{else}}
<div class="large-12">
<p>Sorry, no images to display.</p>
</div>
{{/each}}
</div>
</div><!-- /#media-container -->
</div><!-- /.row -->
<div class="row">
<div id="media-upload" class="large-12">
<div class="buttons">
<button class="btn" {{action "uploadAllFiles"}}>Upload All</button>
<button class="btn" {{action "clearAllFiles"}}>Clear</button>
</div>
{{#if uploadStatus.uploading}}
<h3 class="uploading-percentage">Uploaded Percentage: {{uploadStatus.percentComplete}}%</h3>
{{/if}}
{{#view view.DragDrop}}
{{#if uploadStatus.error}}
<div class="error">An error occurred during the upload process. Please try again later.</div>
{{/if}}
{{#each controller.validFiles}}
<div {{bind-attr class="className:image"}}>
{{name}}
<a class="remove" {{action "deleteFile" this}}>Discard.</a>
{{view view.ImagePreview imageBinding="this"}}
</div>
{{/each}}
{{view view.MultipleInput}}
{{/view}}
</div><!-- /#media-upload -->
</div><!-- /.row -->
media.js
App.MediaController = Ember.ArrayController.extend(DropletController, {
dropletUrl: 'http://localhost:3000/images',
uploadedSomeFiles: function uploadedSomeFiles() {
var files = this.get('uploadedFiles');
files.forEach(function(file) {
// This gets fired every-time a file has uploaded successfully
console.log(file + ' was uploaded!');
});
}.observes('files.length', 'files.@each.uploaded')
});
哦,是的,还有模型,如果有帮助的话(我正在使用 ember-model):
App.Site = Ember.Model.extend({
title: attr(String),
about_text: attr(String),
is_custom_domain: attr(Boolean),
subdomain: attr(String),
url: attr(String),
copyright: attr(String),
site_desc: attr(String),
meta_keywords: attr(String),
footer_code: attr(String),
header_code: attr(String),
hide_site: attr(Boolean),
user: belongsTo('App.User', {key: 'user_id'}),
page: hasMany('App.Page', {key: 'pages', embedded: true}),
media: hasMany('App.Media', {key: 'images', embedded: true})
});
App.Media = Ember.Model.extend({
file_size: attr(Number),
width: attr(Number),
height: attr(Number),
url: attr(String),
identifier: attr(String),
current_path: attr(String),
content_type: attr(String),
thumb: attr(String),
medium: attr(String),
small: attr(String),
tall_banner: attr(String),
short_banner: attr(String),
site: belongsTo('App.Site', {key: 'site_id'})
});
单独使用 jQuery 将新图像附加到页面是非常简单的,但出于某种原因,Ember 使它变得非常复杂。(好吧,平心而论,Ember 的其他事情要容易得多。)
更新:
你不知道吗,我在将它发布到 stackoverflow 后几天(是的,几天)和几个小时都在为一个问题苦苦挣扎,我离答案更近了一点。
我发现由于 emberdroplet 使用标准 XMLHttpRequest,我可以使用 request.onload 并访问我的 Rails API 发回的 request.responseText。
好的,现在这是我的代码:
request.onload = function() {
var media = App.Media.find();
request.responseText.forEach(function(file, media) {
media.pushObject(file);
console.log(file.id);
});
}
但是,我得到了一个错误:
我找回了这个 JSON:
{"id":93,"file_size":438162.0,"width":null,"height":null,"url":"/uploads/image/image/93/blevins_header_2013_v6.jpg", etc.
这被添加到视图中:
<a class="th" data-bindattr-276="276">
<img data-bindattr-278="278">
</a>
所以,我想,Ember-model 不知道如何处理原始 JSON,所以我应该先对它做点什么,对吧?
有任何想法吗?