Mjs

A slice for the Merb framework that offers Ajax actions like RJS with jQuery This privides new 'link_to' method for clients and 'page' object for servers.

Methods

  • Merb::Controller#link_to(name, url='', opts={}) link_to now recognize :remote, :submit option

  • Merb::Controller#page page method is reserved for RJS object. You can access dom elements by it as many times and in anywhere. No more ugly render(:update) block, just type 'page' as you want!

Setup

Include jQuery as usual

app/views/layouts/application.html.erb:

<%= js_include_tag  "jquery.js" -%>

Example1

[ajax get request]

  1. Add :remote option to create ajax link

    app/views/top/index.html.erb:

    <%= link_to "hello", url(:action=>"hello"), :remote=>true %> ^^^^^^^^^^^^^ generates: hello

  2. Use 'page' object to respond as RJS.

    app/controllers/top.rb: def hello page.text "Good morning!" return page end

    generates: $("#message").text("Good morning!");

Example2

[ajax post request]

  1. Add :submit option to specify a DOM element that should be serialized

    app/views/top/index.html.erb:

    <%= text_field ... %> <%= text_area ... %> <%= link_to "update", url(:action=>"update"), :submit=>:edit %> ^^^^^^^^^^^^^ generates: update

  2. enjoy 'page' as you like

    app/controllers/top.rb: def update(id) @item = Item.find(id) ... # update logic is here page.text "successfully saved" update_canvas_for(@item) return page end

    private
      def update_canvas_for(item)
        page[:workspace].html partial("record", :item=>item)
      rescue => error
        page[:message].text error.to_s
        page << "alert('something wrong!')"
      end
    

Copyright (c) 2008 [email protected], released under the MIT license