2015-08-14 57 views
1

更新下面灰燼多sendAction失敗

這裏有一個有趣的一個。我有一個包含窗體組件的模板,而表單組件的模板還包括一個組件:

NewObj.hbs -> obj-form.js/obj-form.hbs -> file-uploader.js 

文件上傳使用ember-uploader和它的偉大工程。我使用它的事件來調用sendAction來告訴obj-form控制器發生的事情,IE傳遞上傳進度百分比並指示上傳完成的時間。

但是,如果我再添加一個sendAction,出於某種原因,obj-form永遠不會收到onComplete的事件。無論我如何嘗試它,它都不會發生 - 在任何地方都不會出錯。

file-uploader組件在OBJ表格模板調用,像這樣:

{{file-uploader 
     url="/uploads" 
     onProgress="fileUploadProgress" 
     onComplete="fileUploadComplete" 
     value=newProjectDownloadFile}} 

file-uploader.js本身看起來是這樣的:

App.FileUploaderComponent = EmberUploader.FileField.extend({ 
    url: '', 

    onProgress: 'onProgress', 
    onComplete: 'onComplete', 

    filesDidChange: function(files) { 
    var uploadUrl = this.get('url'); 
    var that = this; 
    var uploader = EmberUploader.Uploader.create({ 
     url: uploadUrl 
    }); 

    uploader.on('progress', function (e) { 
     that.sendAction('onProgress', e); 
    }); 

    uploader.on('didUpload', function (response) { 
     that.sendAction('onComplete', response); 
    }); 
    if (!Ember.isEmpty(files)) { 
     uploader.upload(files[0]); 
    } 
    } 

}); 

而且obj-form.js在動作這些方法散列:

fileUploadProgress: function (e) { 
    this.set('uploadPercentage', e.percent.toFixed(2)); 
}, 
fileUploadComplete: function (response) { 
    this.set('newProjectDownloadFileUrl', response.url); 
    this.set('newProjectDownloadFileName', response.fileName); 
    this.send('addDownload'); 
}, 

它很好用。但是我想讓進度條只在上傳時出現,在上傳完成後纔會消失。我加了屬性uploadInProgress:假的obj-form控制器,這:

isUploading: function() { 
    return this.uploadInProgress; 
}.property('this.uploadInProgress') 

我用{{#if isUploading}}顯示/隱藏進度條。

我加入:

this.set('uploadInProgress', false); 

obj-formfileUploadComplete()方法,和添加了這個新方法:

fileUploadStart: function() { 
    this.set('uploadInProgress', true); 
}, 

我修改以這個file-uploader組件調用:

{{file-uploader 
     url="/connect/projectDownloads/file" 
     onStart="fileUploadStart" 
     onProgress="fileUploadProgress" 
     onComplete="fileUploadComplete" 
     value=newProjectDownloadFile}} 

而改成file-uploader.js看起來像這樣:

App.FileUploaderComponent = EmberUploader.FileField.extend({ 
    url: '', 

    onStart: 'onStart', 
    onProgress: 'onProgress', 
    onComplete: 'onComplete', 

    filesDidChange: function(files) { 
    var uploadUrl = this.get('url'); 
    var that = this; 
    var uploader = EmberUploader.Uploader.create({ 
     url: uploadUrl 
    }); 

    uploader.on('progress', function (e) { 
     that.sendAction('onProgress', e); 
    }); 

    uploader.on('didUpload', function (response) { 
     that.sendAction('onComplete', response); 
    }); 
    if (!Ember.isEmpty(files)) { 
     that.sendAction('onStart', true); 
     uploader.upload(files[0]); 
    } 
    } 

}); 

我試圖把that.sendAction('onStart', true);位在各種場所中file-uploader.js,但如果該行存在,那麼obj-form控制器從未收到onComplete行動。我完全不知道爲什麼。我把這條線拿出來,它再次運作。

UPDATE ok我發現了一些新東西。這不是破壞它的sendAction,這是此行的OBJ-form.js:

this.set('uploadInProgress', true); 

出於某種原因,當我設置爲true,這一切都分崩離析。我想知道如果我正在嘗試做這件事的方式做錯了什麼?

難道這與didInsertElement有關嗎?我注意到,當我將uploadInProgress設置爲true時會觸發 - 因爲該屬性用於確定進度欄是否出現在頁面上。所以也許我錯誤地使用了didInsertElement?這裏是我的didInsertElement和willDestroyElement:

didInsertElement: function() { 
    // console.log('didInsertElement'); 

    this.set('postType', this.get('content').get('postType')); 
    this.set('projectDownloads', this.get('content').get('projectDownloads')); 
    this.set('projectLinks', this.get('content').get('projectLinks')); 
    this.set('published', this.get('content').get('published')); 
    Ember.addObserver(this.get('content'), 'postType', this, this.postTypeDidChange); 
    Ember.addObserver(this.get('content'), 'projectDownloads', this, this.projectDownloadsDidChange); 
    Ember.addObserver(this.get('content'), 'projectLinks', this, this.projectLinksDidChange); 
    Ember.addObserver(this.get('content'), 'published', this, this.publishedDidChange); 
}, 

    willDestroyElement: function() { 
    console.log('willDestroyElement'); 
    Ember.removeObserver(this.get('content'), 'postType', this.postTypeDidChange); 
    Ember.removeObserver(this.get('content'), 'projectDownloads', this.projectDownloadsDidChange); 
    Ember.removeObserver(this.get('content'), 'projectLinks', this.projectLinksDidChange); 
    Ember.removeObserver(this.get('content'), 'published', this.publishedDidChange); 
    } 

我試圖修改isUploading給你推薦的別名的風格,但是這並沒有幫助任何東西。這個想法是什麼時候設置爲true,它會使進度條出現,並且文件上載形式消失。然後,當它被設置爲false時,會發生相反的情況。我願意以其他方式在Ember中實現這一點,我無法弄清楚我正在做的事情是否會破壞事情。

+0

是否'didUpload'回調得到調用? – Kingpin2k

+0

file-uploader.js上的Yep didUpload被調用,但obj-form.js上的fileUploadComplete沒有收到該操作。 –

回答

1

還沒有答案,但不適合正確評論... this在依賴鏈中不是必需的,更容易的是將其更改爲別名。

isUploading: function() { 
    return this.uploadInProgress; 
}.property('this.uploadInProgress') 

isUploading: Ember.computed.alias('uploadInProgress')