从ember uploader S3 url设置模型属性?

时间:2015-05-11 10:32:16

标签: ember.js amazon-s3

我有ember-uploader将文件成功上传到S3。当图像上传完成后,我想将模型属性image_url设置为返回的URL,然后最好提交表单以创建记录。我该怎么做?

应用程序/模型/ post.js:

import DS from 'ember-data';                                                                                                                                             

export default DS.Model.extend({                                                                                                                                         
  name: DS.attr('string'),                                                                                                                                               
  body: DS.attr('string'),                                                                                                                                               
  image_url: DS.attr('string')                                                                                                                                           
});         

应用程序/模板/帖/ new.hbs:

<div class="new-post-form">                                                                                                                                              
  {{input type="text" placeholder="Name" value=name}}                                                                                                                    
  {{input type="text" placeholder="Message" value=body}}                                                                                                                 
  {{s3-upload value=image_url}}                                                                                                                                          
  <button {{action 'addPost'}} class="submit">Submit</button>                                                                                                            
</div>       

应用/组件/ S3-upload.js:

import Ember from 'ember';                                                                                                                                               
import EmberUploader from 'ember-uploader';                                                                                                                              

export default EmberUploader.FileField.extend({                                                                                                                          
  url: 'http://localhost:3000/sign',                                                                                                                                     

  filesDidChange: (function() {                                                                                                                                          
    var uploadUrl = this.get('url');                                                                                                                                     
    var files = this.get('files');                                                                                                                                       

    var uploader = EmberUploader.S3Uploader.create({                                                                                                                     
      url: uploadUrl                                                                                                                                                     
    });                                                                                                                                                                  

    uploader.on('didUpload', function(response) {                                                                                                                        
      // S3 will return XML with url                                                                                                                                     
      var uploadedUrl = Ember.$(response).find('Location')[0].textContent;                                                                                               
      uploadedUrl = decodeURIComponent(uploadedUrl); // => http://yourbucket.s3.amazonaws.com/file.png                                                                   
      console.log("UPLOADED ! : " + uploadedUrl);                                                                                                                        
    });                                                                                                                                                                  
                                                                                                                                                                             if (!Ember.isEmpty(files)) {                                                                                                                                         
      uploader.upload(files[0]); // Uploader will send a sign request then upload to S3                                                                                      }                                                                                                                                                                    
  }).observes('files')                                                                                                                                                   
});  

正如您所看到的,我尝试将我的s3-uploader组件的value设置为image_url,但似乎没有做任何事情。

0 个答案:

没有答案