【问题标题】:Mercury Editor 'saved' event not firingMercury 编辑器“已保存”事件未触发
【发布时间】:2015-09-04 02:59:31
【问题描述】:

我的 Mercury Gem - 0.9.0 版

gem 'mercury-rails', git: 'https://github.com/jejacks0n/mercury.git'

我正在使用 Rails4

最初遵循 RailsCast 我在 Mercury.js 中有以下内容:

$(window).bind('mercury:ready', function() {
  var link = $('#mercury_iframe').contents().find('#edit_link');//extract the saveURL for mercury that was encoded in the HTML data tag
  Mercury.saveUrl =link.data('save-url');
  console.log("Mercury save URL: "+Mercury.saveUrl);
  link.hide();//hide edit url
});

$(window).bind('mercury:saved', function() {
  console.log("mercury saved event set");
  window.location.href = window.location.href.replace(/\/editor\//i, '/');
});

然而,RailsCast cmets 说现在应该重写 onload 函数:

window.Mercury = {
  //...

  //CUSTOM CODE
  onload:function(){
    Mercury.on('saved',function(){
        console.log("SAVE EVENT FIRED");
        window.location.href = window.location.href.replace(/\/editor\//i, '/');            
    });
    Mercury.on('ready',function(){
        var link = $('#mercury_iframe').contents().find('#edit_link');//extract the saveURL that was encoded in the HTML data tag
        Mercury.saveUrl =link.data('save-url');
        console.log("Mercury save URL: "+Mercury.saveUrl);
        link.hide();//hide edit url
    });
  }
};

但是,保存的事件永远不会被触发,并且重定向永远不会发生。关于新的和改进的做事方式有什么想法吗?谢谢!

编辑:澄清一下,'Mercury.on('ready'...)' 事件确实成功触发。

编辑:好的,所以我发现“已保存”事件适用于一条路线,但不适用于另一条路线。

#Works on this route! 
def mercury_update_courses
    courses_infos = params[:content]
    courses_infos.each{|course_info|
        label = course_info[0]
        new_val = course_info[1][:value]

        id = label.delete("^0-9")#find the database id by removing everything that's not a number from the HTML element's CSS id
        course = EditablePage.where("id == ?",id).last

        if label.index("title") != nil
            course.title = new_val
        elsif label.index("body") != nil
            course.body = new_val
        end

        course.save!
    }
    render text: ""
end

#does not work on this route!
def mercury_update_index
    homepage = EditablePage.find(params[:id])
    homepage.body = params[:content][:homepage_content][:value]
    homepage.save!
    render text: ""
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 content-management-system mercury-editor


    【解决方案1】:

    发现问题!毕竟,保存事件实际上是有效的。

    console.log("SAVE EVENT FIRED");
    

    没有出现,因为它被放置在重定向之前,这会擦除 JS 控制台。在我发现它在一个页面而不是另一个页面上工作后,我试图找出这些页面的不同之处,并发现问题出在 URL 中。

    #working
      http://localhost:3000/editor/courses
    #not working
      http://localhost:3000/editor
    

    因此问题在于我从我遵循的教程中复制+粘贴的神秘正则表达式

    window.location.href = window.location.href.replace(/\/editor\//i, '/');
    

    是罪魁祸首。非工作编辑器位于站点的根目录/主页上,因此在 url 的“/editor/”部分中不包含第二个“/”。我通过制作更通用(且更易于理解)的替换行来解决此问题

    window.location.href = window.location.href.replace("/editor", '');
    

    最终代码:

    window.Mercury = {
      //...
    
      //CUSTOM CODE
      onload:function(){
        Mercury.on('saved',function(){
            window.location.href = window.location.href.replace("/editor", '');
            console.log("Mercury info saved!"); 
        });
        Mercury.on('ready',function(){
            var link = $('#mercury_iframe').contents().find('#edit_link');//extract the saveURL that was encoded in the HTML data tag
            Mercury.saveUrl =link.data('save-url');
            console.log("Mercury save URL: "+Mercury.saveUrl);
            link.hide();//hide edit url when mercury is open
        });
      }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 2018-04-28
      • 2022-11-27
      • 1970-01-01
      • 2011-10-07
      相关资源
      最近更新 更多