简体   繁体   English

如何让Mercury Editor重定向到新资源?

[英]How do I get Mercury Editor to redirect to a new resource?

After watching RailsCast #296 about Mercury Editor , I am trying to get the editor to redirect to a newly created resource. 在观看关于Mercury Editor的RailsCast#296后,我试图让编辑器重定向到新创建的资源。

I can already redirect on the client-side using JS and window.location.href= . 我已经可以使用JS和window.location.href=在客户端重定向。 But for a new resource, I cannot "guess" its URL on the client-side. 但是对于新资源,我不能在客户端“猜测”它的URL。 I need it to be in the server response. 我需要它在服务器响应中。

However, the problem is that I don't see the possibility of using the server response in the editor. 但是,问题是我没有看到在编辑器中使用服务器响应的可能性。 No matter what the controller renders, the server response is discarded by Mercury instead of used as an argument to my function for mercury:saved . 无论控制器呈现什么,服务器响应都 Mercury 丢弃 ,而不是用作我的mercury:saved函数的参数mercury:saved

Is there a way to get around this? 有办法解决这个问题吗?

I was able to do this on update by sending a valid JSON string back. 通过发送有效的JSON字符串,我能够在更新时执行此操作。 I would assume create works the same way. 我认为创建工作方式相同。 check firebug to make sure you're not getting an error in the jQuery.ajax call that Mercury uses. 检查firebug以确保您没有在Mercury使用的jQuery.ajax调用中收到错误。

posts_controller.rb posts_controller.rb

def mercury_update
  post = Post.find(params[:id])
  post.title = params[:content][:post_title][:value]
  post.body = params[:content][:post_body][:value]
  post.save!
  render text: '{"url":"'+ post_path(post.slug) +'"}'
end

mercury.js: mercury.js:

  jQuery(window).on('mercury:ready', function() {
    Mercury.on('saved', function() {
       window.location.href = arguments[1].url      
    });
  });

note: I'm using friendly_id to slug my posts 注意:我正在使用friendly_id来发布帖子

Redirecting on the server side doesn't work because the save button is just an jQuery.ajax call: 在服务器端重定向不起作用,因为保存按钮只是一个jQuery.ajax调用:

// page_editor.js
PageEditor.prototype.save = function(callback) {
      var data, method, options, url, _ref, _ref1,
        _this = this;
      url = (_ref = (_ref1 = this.saveUrl) != null ? _ref1 : Mercury.saveUrl) != null ? _ref : this.iframeSrc();
      data = this.serialize();
      data = {
        content: data
      };
      if (this.options.saveMethod === 'POST') {
        method = 'POST';
      } else {
        method = 'PUT';
        data['_method'] = method;
      }
      Mercury.log('saving', data);
      options = {
        headers: Mercury.ajaxHeaders(),
        type: method,
        dataType: this.options.saveDataType,
        data: data,
        success: function(response) {
          Mercury.changes = false;
          Mercury.trigger('saved', response);
          if (typeof callback === 'function') {
            return callback();
          }
        },
        error: function(response) {
          Mercury.trigger('save_failed', response);
          return Mercury.notify('Mercury was unable to save to the url: %s', url);
        }
      };
      if (this.options.saveStyle !== 'form') {
        options['data'] = jQuery.toJSON(data);
        options['contentType'] = 'application/json';
      }
      return jQuery.ajax(url, options);
    };

So your redirect is sent to the success callback, but the page doesn't actually re-render, as with any successful AJAX request. 因此,您的重定向将发送到success回调,但该页面实际上不会重新呈现,就像任何成功的AJAX请求一样。 The author discusses overriding this very function here . 作者在这里讨论了覆盖这个功能。 It also looks like there might be some room to maneuver here by passing a callback function to save . 通过传递回调函数来save可能还有一些空间可供操作。

Btw, another way to do what @corneliusk suggests is: 顺便说一句,@ corneliusk建议的另一种方法是:

render { json: {url: post_path(post.slug)} }

Either way, the response body is passed as an argument to the function in the mercury:saved callback. 无论哪种方式,响应主体都作为参数传递给mercury:saved回调中的函数。

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

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