如何在#34;这个"成为"那"

时间:2015-07-16 00:46:13

标签: polymer polymer-1.0

我正在尝试将dropzone.js和cloudinary集成到Polymer 1.0中。它确实有效,但我遇到了如何将Cloudinary生成的动态URL发送回Polymer的绊脚石,这样我就可以将该URL写入Firebase。我在一个功能内部监听dropzone事件,目的是使用铁信号来发信号通知不同的Web组件。 "这"现在的范围是dropzone.js而不是Polymer。

..导致" Uncaught TypeError:this.fire不是函数"。

代码在下面,我正在尝试基于收听dropzone.js"成功"来启动铁信号。提供对新图像URL的访问的事件。

<link rel="stylesheet" href="../../../bower_components/dropzone/dist/min/dropzone.min.css">

<dom-module id="my-dropzone">

  <style>
    :host {
      display: block;
    }

    div#my-dropzone-area {
      max-width=300px;
      height=300px;
      border: 4px dashed blue;
    }
  </style>

  <template>

    <paper-button on-tap="startTheMessage">Test Fire!</paper-button>
    <iron-signals on-iron-signal-hello="passTheMessage">

      <div class="dropzone" id="my-dropzone-area">
        <div class="fallback">
          <input name="file" type="file" multiple />
        </div>
      </div>

  </template>
</dom-module>

<script>
  (function() {
    Polymer({
      is: 'my-dropzone',
      ready: function() {
        // access a local DOM element by ID using this.$
        Dropzone.options.myDropzoneArea = {
          paramName: 'file', // The name that will be used to transfer the file
          maxFilesize: 10, // MB
          uploadMultiple: false,
          acceptedFiles: '.jpg,.png,.jpeg,.gif',
          parallelUploads: 6,
          addRemoveLinks: true,
          url: 'https://api.cloudinary.com/v1_1/remarkable-ky/image/upload',
          init: function() {
            this.on('addedfile', function(file) {
              console.log('Added file.');
              console.log(file);
            });
            this.on('sending', function(file, xhr, formData) {
              console.log('Sending file.');
              formData.append('api_key', 0000000000000);
              formData.append('timestamp', Date.now() / 1000);
              formData.append('upload_preset', 'where-ky');
            });
            this.on('success', function(file, response) {
              var baseURL = 'http://res.cloudinary.com/remarkable-ky/image/upload/';
              var url = baseURL.concat(response.public_id);
              console.log('Cloudinary URL: ', url);
              this.fire('iron-signal', {
                name: 'hello',
                data: null
              });
            });
          }
        };
      },
      startTheMessage: function() {
        this.fire('iron-signal', {
          name: 'hello',
          data: null
        });
      },
      passTheMessage: function() {
        alert("got it");
      },
      properties: {},
    });
  })();
</script>

<script src="../../../bower_components/dropzone/dist/min/dropzone.min.js"></script>

1 个答案:

答案 0 :(得分:2)

您可以使用.bind()函数将其传递给函数。

        this.on('success', function(file, response) {
          var baseURL = 'http://res.cloudinary.com/remarkable-ky/image/upload/';
          var url = baseURL.concat(response.public_id);
          console.log('Cloudinary URL: ', url);
          this.fire('iron-signal', {
            name: 'hello',
            data: null
          });
        }.bind(this));