merb jquery

56
Merb with jQuery (not RJS)

Upload: yehuda-katz

Post on 18-May-2015

5.011 views

Category:

Technology


0 download

DESCRIPTION

Using jQuery with Merb (and why Merb doesn't do RJS)

TRANSCRIPT

Page 1: Merb jQuery

Merb with jQuery

(not RJS)

Page 2: Merb jQuery

Rails and JS

Page 3: Merb jQuery

<% remote_form_for(@user) do |f| %> <%= f.text_field :username %><% end %>

Page 4: Merb jQuery

<form action="/users" class="new_user" id="new_user" method="post" onsubmit="new Ajax.Request('/users', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="dcab4e1ab6a78f1f618b97008246df3334075ca9" /></div> <input id="user_username" name="user[username]" size="30" type="text" /></form>

inline JS

Page 5: Merb jQuery

<center> <font face="Arial">Hello</font></center>

Page 6: Merb jQuery

<center> <font face="Arial">Hello</font></center>

we stopped doing this

Page 7: Merb jQuery

“It works”

Page 8: Merb jQuery

Kinda.

Page 9: Merb jQuery

Difficulty

Time in Project

Page 10: Merb jQuery

Difficulty

Time in Project

remote_form_for... fantastic! so easy!

Page 11: Merb jQuery

Difficulty

Time in Project

autocomplete! wow! I don’t need

any JS!

Page 12: Merb jQuery

Difficulty

Time in Project

holy cow... RJS... Who needs this stodgy

“Javascript”

Page 13: Merb jQuery

Difficulty

Time in Project

one day...

Page 14: Merb jQuery

Difficulty

Time in Project

we need an autocompleter that talks

to a web service

Page 15: Merb jQuery

Difficulty

Time in Project

quick... cancel the launch

Page 16: Merb jQuery

Difficulty

Time in Project

Page 17: Merb jQuery

Or...

Page 18: Merb jQuery

Difficulty

Time in Project

Page 19: Merb jQuery

Difficulty

Time in Project

learn JS or hire someone who

knows JS

Page 20: Merb jQuery

“It’s hard”

Page 21: Merb jQuery

Let’s take a look

Page 22: Merb jQuery

<%= form_for(@user, :class => "remote", :action => "/foo") do %> <%= text_field :username %><% end =%>

Page 23: Merb jQuery

<form class="remote" method="post" action="/foo"> <input type="text" class="text" value="" name="username" id="params_username"/></form>

Page 24: Merb jQuery

And the JavaScript

Page 25: Merb jQuery

(scary!)

Page 26: Merb jQuery

$("form.remote").ajaxForm();

Page 27: Merb jQuery

link_to_remote

Page 28: Merb jQuery

<%= link_to_remote "Awesome!", :url => "/foo", :update => "some_id" %>

Page 29: Merb jQuery

<a href="#" onclick="new Ajax.Updater('some_id', '/foo', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('dcab4e1ab6a78f1f618b97008246df3334075ca9')}); return false;">Awesome!</a>inline JS!

Page 30: Merb jQuery

<a href="#" onclick="new Ajax.Updater('some_id', '/foo', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('dcab4e1ab6a78f1f618b97008246df3334075ca9')}); return false;">Awesome!</a>(so easy!)

Page 31: Merb jQuery

<%= link_to "Awesome!", "/foo", :rel => "#some_id", :class => "remote" %>

Page 32: Merb jQuery

<a href="/foo" class="remote" rel="#some_id">Awesome!</a>

Page 33: Merb jQuery

“I bet the JavaScript is super-crazy”

Page 34: Merb jQuery

$("a.remote").click(function() { if(this.rel) $(this.rel).load(this.href); else $.get(this.href); return false;})

Page 35: Merb jQuery

$("a.remote").click(function() { if(this.rel) $(this.rel).load(this.href); else $.get(this.href); return false;})

CSS selector

Page 36: Merb jQuery

$("a.remote").click(function() { if(this.rel) $(this.rel).load(this.href); else $.get(this.href); return false;})

supplying a rel means update

Page 37: Merb jQuery

$("a.remote").click(function() { if(this.rel) $(this.rel).load(this.href); else $.get(this.href); return false;})

otherwise use $.get

Page 38: Merb jQuery

$("a.remote").click(function() { if(this.rel) $(this.rel).load(this.href); else $.get(this.href); return false;}) X

no JS?

Page 39: Merb jQuery

<a href="/foo" class="remote" rel="#some_id">Awesome!</a>

Page 40: Merb jQuery

Want a helper?

Page 41: Merb jQuery

def link_to_remote(text, url, options = {}) if options.key?(:update) options[:rel] = options.delete(:update) end options[:class] = options[:class].to_s. split(" ").push("remote").join(" ") link_to(text, url, options)end

Page 42: Merb jQuery

If you switch to Prototype (o.O)

Page 43: Merb jQuery

$$("a.remote").observe("click", function() { if(this.rel) new Ajax.Updater($$(this.rel), this.href); else new Ajax.Request(this.href); return false;});

Page 44: Merb jQuery

Using JSON

Page 45: Merb jQuery

class Speakers < Application provides :json

def index @speakers = Speaker.all display @speakers end end

Page 46: Merb jQuery

class Speakers < Application provides :json

def index @speakers = Speaker.all display @speakers end end

Page 47: Merb jQuery

$.getJSON("/speakers", function(json) { // [{id: 1, name: "Joe Blow"}]});

Page 48: Merb jQuery

$.getJSON("/speakers", function(json) { // [{id: 1, name: "Joe Blow"}]});

Accept: application/json

Page 49: Merb jQuery

class Speakers < Application provides :json

def index @speakers = Speaker.all display @speakers end end @speakers.to_json

Page 50: Merb jQuery

What about RJS?

Page 51: Merb jQuery

page.insert_html :bottom, 'list', content_tag("li", "Fox")

Page 52: Merb jQuery

Generated JS

Page 53: Merb jQuery

try { new Insertion.Bottom("list", "<li>Fox</li>");} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('new Insertion.Bottom(\"list\", \"<li>Fox</li>\");'); throw e }

Page 54: Merb jQuery

Big Scary JS

Page 55: Merb jQuery

$.get("/ajax_url", function(html) { $("#list").append(html)})

Page 56: Merb jQuery

Thank you.