2015-05-11 95 views
0

我正在關注this tutorial將現有文件嘲諷爲啓用Dropzone的表單,但遇到掛鉤到init函數的問題。這是我迄今爲止:我console.log()Dropzone:綁定到初始化

var imageUpload; 

if ($('.js-listing__images-upload').length) { 
    imageUpload = new Dropzone('.js-listing__images-upload', { 
    addRemoveLinks: true, 
    maxFiles: 5, 
    maxFilesize: 3, 
    acceptedFiles: 'image/jpeg,image/png,image/gif' 
    }); 
    imageUpload.on('init', function() { 
    var listingID, thisDropzone; 
    console.log('dropzone init'); 
    thisDropzone = this; 
    listingID = $('#manage-images').data('listing'); 
    $.ajax({ 
     url: dashboard.images.list, 
     data: { 
     listing: listingID 
     }, 
     success: function(data) { 
     console.log('ajax for list done'); 
     $.each(data, function(key, value) { 
      var mockFile; 
      mockFile = { 
      name: value.name, 
      size: value.size 
      }; 
      thisDropzone.options.addedfile.call(thisDropzone, mockFile); 
      thisDropzone.options.thumbnail.call(thisDropzone, mockFile, '/uploads/thumb/listings/' + value.name); 
     }); 
     } 
    }); 
    }); 
} 

無,但我不能確定至於什麼問題可能是。在Dropzone的配置之後,我應該可以使用簡單的on機制來掛鉤事件。謝謝大家!

更新

下不工作:

# When images are removed 
# Dropzone 
imageUpload.on 'removedfile', (file) -> 
    if file.xhr 
    imageID = JSON.parse file.xhr.response 
    $.ajax 
     url: dashboard.images.destroy 
     data: { image: imageID } 
     success:() -> 
    return 

所以它的一些事我遇到麻煩init功能。

+0

什麼是'$( 'JS-listing__images上傳。')length'評估來?另外,當你運行這個代碼時DOM是否準備好了?這是在你的'$(function(){// Code here})'? – arjabbar

+0

嗨@arjabbar我對'.js-listing__images-upload'的檢查評估爲true;所以我們很好。我把所有的東西包裝在一個自我激勵的匿名函數中。使用它的任何問題?謝謝! – Zach

回答

0

init功能可以直接綁定到新的懸浮窗實例:

if $('.js-listing__images-upload').length 
    imageUpload = new Dropzone(
    '.js-listing__images-upload', 
    addRemoveLinks: true 
    maxFiles: 5 
    maxFilesize: 3 
    acceptedFiles: 'image/jpeg,image/png,image/gif' 
    init: -> 
     thisDropzone = this 
     $.ajax 
     url: dashboard.images.list 
     data: { listing: $('#manage-images').data('listing') } 
     success: (data) -> 
      $.each data, (key, value) -> 
      mockFile = 
       name: value.name 
       size: value.size 
      thisDropzone.options.addedfile.call thisDropzone, mockFile 
      thisDropzone.options.thumbnail.call thisDropzone, mockFile, '/uploads/thumb/listings/' + value.name 
      return 
      return 
     return 
)