简体   繁体   English

在添加文件之前进行验证(自定义)

[英]Validate (custom) before adding file

I would like to make sure if the user is logged-in or not before anything is added to the uploader area (thumbnails). 在将任何内容添加到上传器区域(缩略图)之前,我想确保用户是否已登录。

it's easy enough checking stuff on the validate event callback, but I can't seem to figure out how to stop the item from being added to the DOM, even if I return false in my validation or submit events...the item just gets a class of qq-upload-fail ... but I want to completly do NOTHING if the user isn't logged in...maybe the validation event handler is not the place to put this logic, but where else? 检查validate事件回调上的内容很容易,但是我似乎无法弄清楚如何阻止将该项目添加到DOM中,即使我在验证或submit事件中返回false也是如此。一类qq-upload-fail ...但是如果用户未登录,我想完全不做...也许验证事件处理程序不是放置此逻辑的地方,但是还有其他地方吗?


My initialization code (jQuery): 我的初始化代码(jQuery):

 this.holderElm.fineUploader({ template: 'qq-simple-thumbnails-template', // uploaderType: 'basic', request: { endpoint: '/image/upload_image/' + this.settings.uploadID }, validation: { allowedExtensions : ['jpeg', 'jpg', 'gif', 'png'], sizeLimit : this.settings.sizeLimit, itemLimit : this.settings.itemsLimit }, retry: { enableAuto: true } }) .on("validate", this.onValidate.bind(this) ) .on('submit', this.onSubmit.bind(this) ) .on('error', this.onError.bind(this) ) .on('upload', this.onUpload.bind(this) ) .on('complete', this.onComplete.bind(this) ) .on('allComplete', this.onAllComplete.bind(this) ); 

Use the onSubmit event -- which called is before the item has been added to the DOM -- and return false or a qq.Promise that will be rejected to disable the addition of that item to the file list. 使用onSubmit事件 (在将该项目添加到DOM之前调用),然后返回falseqq.Promise ,该事件将被拒绝以禁止将该项目添加到文件列表中。

var userLoggedIn = true;

// snippet ...    

onSubmit: function(id, name){
    return userLoggedIn;
},

or with promises: 或承诺:

function isUserLoggedIn(username){
  var promise = new qq.Promise();

  $.get("/api/user/"+username, function(err, data){
      if (err) return promise.failure(err);
      return promise.success(data);
  });

  return promise;
}

// snippet ...  

onSubmit: function(id, name){
    return isUserLoggedIn(username);
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM