首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >水星编辑器“保存”事件未被触发

水星编辑器“保存”事件未被触发
EN

Stack Overflow用户
提问于 2015-09-04 02:59:31
回答 1查看 65关注 0票数 0

我的水星宝石-版本0.9.0

代码语言:javascript
复制
gem 'mercury-rails', git: 'https://github.com/jejacks0n/mercury.git'

我正在使用Rails4

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

代码语言:javascript
复制
$(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注释指出,现在应该重写onload函数:

代码语言:javascript
复制
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”(“就绪”.)事件确实成功地触发了。

编辑:好的,所以我发现“保存”事件使用的是一条路线,而不是另一条。

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-15 16:55:38

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

代码语言:javascript
复制
console.log("SAVE EVENT FIRED");

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

代码语言:javascript
复制
#working
  http://localhost:3000/editor/courses
#not working
  http://localhost:3000/editor

因此,问题就在我下面的教程中的神秘正则表达式i copy+pasted中。

代码语言:javascript
复制
window.location.href = window.location.href.replace(/\/editor\//i, '/');

是罪魁祸首不工作的编辑器位于站点的根/主页上,因此在url的'/ editor /‘部分中没有包含第二个"/“。我修正了这一点,做了一个更一般(更容易理解)的替换行。

代码语言:javascript
复制
window.location.href = window.location.href.replace("/editor", '');

最终代码:

代码语言:javascript
复制
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
    });
  }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32389399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档