简体   繁体   English

Rails + jQuery:更新容器后裁剪不起作用

[英]Rails + jQuery: cropping doesn't work after update container

I have a question regarding updating a container. 我有一个有关更新容器的问题。 I have an image, which is resized in the background using a delayed job. 我有一张图像,该图像在后台使用延迟的作业进行了调整。 On finish, it sets an attribute to false. 最后,它将属性设置为false。 The view is checking this attribute each 500ms. 视图每500ms检查一次此属性。 After completing, the view should update the image and give the user a crop-box. 完成后,视图应更新图像并为用户提供一个裁剪框。

So far everything looks like: 到目前为止,一切看起来像:

<div id='edit_image'>
  <div class='image'>
    <img alt="spinner" src="/images/spinner.gif" />
  </div>
</div>


    $(window).load(function() {

        var jcrop_api;
        image_crop = $.Jcrop('#imagecrop', 
        {
           aspectRatio: 430 / 575,
           trueSize: [#{@asset.image.width}, #{@asset.image.height}],
           minSize: [430, 575],
           allowSelect: false,
           onChange: updateCrop,
           onSelect: updateCrop      
        });
        imagecrop_api.setSelect(#{to_jcrop(@asset.crop)});

    });

    function updateCrop(c) {
      $('#image_crop').val(c.w +"x"+ c.h +"+"+ c.x +"+"+ c.y);
    };

    $(document).ready(function() {
      var refresh_timer = setInterval(check_image_status, 200);

      function check_image_status() {
        $.post('/assets/' + "#{@asset.id}" + '/asset_resizing',
          function(data) {
            if (data == false) {
              $('.image').html('#{ image_tag @asset.image.url, :id => 'edit_image', :class => 'image'}');
              clearInterval(refresh_timer);
            } else {
              return false;
            }
        });
      };
    });

The image is successfully loaded and the container updated. 图像已成功加载并且容器已更新。 The only problem is, that I can't crop anymore after that. 唯一的问题是,此后我再也无法收割了。 If I load the image directly, the cropping works. 如果我直接加载图像,则可以进行裁剪。 Thanks for advice. 谢谢你的建议。

Hey. 嘿。 If you render a update.js.erb to handle the response from the server, I guess you'll have to put this code in the update.js.erb file. 如果您呈现一个update.js.erb来处理服务器的响应,我想您必须将这段代码放在update.js.erb文件中。

$(window).load(function() {

        var jcrop_api;
        image_crop = $.Jcrop('#imagecrop', 
        {
           aspectRatio: 430 / 575,
           trueSize: [#{@asset.image.width}, #{@asset.image.height}],
           minSize: [430, 575],
           allowSelect: false,
           onChange: updateCrop,
           onSelect: updateCrop      
        });
        imagecrop_api.setSelect(#{to_jcrop(@asset.crop)});

    });

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

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