Rails is not just Ruby
- 7. Know your tools!
var inputs = $$('input');
for (var i = 0; i < inputs.length; i++) {
alert(inputs[i].name);
}
- 8. Know your tools!
X
var inputs = $$('input');
for (var i = 0; i < inputs.length; i++) {
}
alert(inputs[i].name);
- 10. Know your tools!
function setupPage() {
$('userName').update(
readUserName();
);
}
function readUserName() {
//read user's name from cookie
}
- 11. Know your tools!
X
function setupPage() {
$('userName').update(
readUserName();
);
}
function readUserName() {
//read user's name from cookie
}
- 12. Know your tools!
var Application = (function() {
//private
var readUserName = function() {
//read user's name from cookie
};
return {
//public
setupPage: function() {
$('userName').update(
readUserName();
);
}
}
})();
- 13. Know your tools!
var timeout = window.setTimeout(
"element.update(" + someContent
+ ");",
1000
);
- 14. Know your tools!
X
var timeout = window.setTimeout(
"element.update(" + someContent
+ ");",
1000
);
- 16. Know your tools!
var loginField =
document.getElementById('#user_login');
alert(loginField.value);
- 17. Know your tools!
X
var loginField =
document.getElementById('#user_login');
alert(loginField.value);
- 19. Know your tools!
var loginField =
document.getElementById('#user_login');
loginField.style.display = 'none';
- 20. Know your tools!
X
var loginField =
document.getElementById('#user_login');
loginField.style.display = 'none';
- 22. Know your tools!
var loginField =
document.getElementById('user_login');
function loginChanged() {
alert(loginField.value);
}
if (loginField.addEventListener) {
loginField.addEventListener(
'change', loginChanged, false);
} else if (obj.attachEvent) {
obj.attachEvent('onchange', loginChanged);
}
- 23. Know your tools!
X
var loginField =
document.getElementById('user_login');
function loginChanged() {
alert(loginField.value);
}
if (loginField.addEventListener) {
loginField.addEventListener(
'change', loginChanged, false);
} else if (obj.attachEvent) {
obj.attachEvent('onchange', loginChanged);
}
- 36. the Demo App
POST/replace
Code is at http://github.com/marcoow/js-and-rails
- 44. the classic Solution
•strong coupling
•hard to maintain
•no/ little code reuse
•bloated HTML
•code that actually belongs together is
distributed over several places
•easy to write in the first place
- 46. Full Separation
•define JavaScript controls that encapsulate
all frontend logic
•mark elements with class, rel or HTML5‘s
data-* attributes
•full separation of HTML and JavaScript
•Initialization of controls on dom:loaded
event
- 47. Full Separation
•define JavaScript controls that encapsulate
all frontend logic
•mark elements with class, rel or HTML5‘s
data-* attributes
•full separation of HTML and JavaScript
•Initialization of controls on dom:loaded
event
- 48. Full Separation
replacer.js
var Replacer = Class.create({
initialize: function(container, target) {
this.container = $(container);
this.target = $(target);
this.container.observe('click', this.onClick.bindAsEventListener(this));
},
onClick: function(event) {
event.stop();
new Ajax.Updater(
this.target,
this.container.href, {
method: 'post',
evalScripts: true
}
);
}
});
- 49. Full Separation
application.js
var Application = (function() {
var initializeReplacers = function() {
$$('a[data-replaces]').each(function(replacingLink) {
if (!replacingLink._initializedReplacer) {
new Replacer(replacingLink, replacingLink.readAttribute('data-replaces'));
replacingLink._initializedReplacer = true;
}
});
};
return {
setupOnLoad: function() {
initializeReplacers();
},
setupOnPageUpdate: function() {
initializeReplacers();
}
}
})();
- 58. explicit Controls
index.html.erb
<div id="someElement">
some text that's replaced later
</div>
<%= link_to 'Replace', controls_replace_path,
:id => 'replacerLink' %>
<script type="text/javascript" charset="utf-8">
document.observe('dom:loaded', function() {
var replacer = new Replacer('replacerLink', 'someElement');
});
</script>
- 60. explicit Controls
_new_content.html.erb
<b>Fresh new content rendered at
<%= Time.now %></b>
<%= link_to 'Replace again', controls_replace_path,
:id => 'secondReplacerLink' %>
<script type="text/javascript" charset="utf-8">
var newReplacer = new Replacer(
'secondReplacerLink', 'someElement'
);
</script>
- 61. explicit Controls
_new_content.html.erb
<b>Fresh new content rendered at
<%= Time.now %></b>
<%= link_to 'Replace again', controls_replace_path,
:id => 'secondReplacerLink' %>
<script type="text/javascript" charset="utf-8">
var newReplacer = new Replacer(
'secondReplacerLink', 'someElement'
);
</script>
No initialization on dom:loaded here as this is
the result of an AJAX request (dom:loaded not
fired)
- 62. explicit Controls
•HTML is (mostly) clean and semantic
•Behaviour is explicit in the templates
•easier to grasp what‘s going on than with
Full Separation
•though not as nice as full separation