简体   繁体   中英

Mercury Editor 'saved' event not firing

My Mercury Gem - version 0.9.0

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

And I am using Rails4

Originally following the RailsCast I had the following in 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, '/');
});

However, the RailsCast comments say that the onload function should be overridden now instead:

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
    });
  }
};

However, the saved event is never fired and that redirect never occurs. Any idea on what the new and improved way is to do things? Thank you!

Edit: To clarify, the 'Mercury.on('ready'...)' event does fire successfully.

Edit: Alright, so I've discovered that the 'saved' event is working with one route, but not another.

#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

Discovered the issue! The save event was actually working after all. The

console.log("SAVE EVENT FIRED");

did not appear as it was placed before a redirect, which wipes the JS console. After I discovered it was working in one page and not the other, I tried to find what was different about the pages, and saw that the issue lied within the URL.

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

Thus the problem lied in the cryptic regex expression I copy+pasted from the tutorial I followed

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

was the culprit. The non-working editor was on the root/homepage of the site, and thus did not include the second "/" in the '/editor/' portion of the url. I fixed this by making a more general (and easier to understand) replace line

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

Final code:

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
    });
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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