4 ms·
So this (with specialized lib, and fairly large unintuitive js code to set whole thing up): <div id="foo" jsaction="leftNav.clickAction; d
by scotty79 13y ago
So this (with specialized lib, and fairly large unintuitive js code to set whole thing up):
<div id="foo" jsaction="leftNav.clickAction;
dblclick:leftNav.doubleClickAction">
is pretty much the same as this:
<div id="foo"
onclick="Actions.leftNav.clickAction()"
ondblclick="Actions.leftNav.doubleClickAction()">
without any abusing or manipulating of html and dom and with setup as simple and understandable as this:
window.Actions = {
leftNav: {
clickAction: function() {
myApp.LeftNav.doSomeSeriousStuff();
},
doubleClickAction: function() {
// very late loading of implementation
require("LeftNavActions", function(LeftNavActions) {
LeftNavActions.doSomeOtherSeriousStuff();
})
},
// if you want add handlers from other places
// with Actions.leftNav._anotherAction.push()
_anotherAction: [],
anotherAction: function() {
this._anotherAction.forEach(function(a) { a(); });
}
}
}
Actions is a good idea that I remember from Delphi 4. It is just one additional layer of indirection that enables you to attach same behavior for example to menu option and toolbar button.
- marcus_holmes 13y agoI like the syntax, but it bugs me that the behaviour goes into the HTML. I really like keeping my event mapping in the javascript so I keep a clean separation between structure in the HTML and behaviour in the JS.
- Kiro 13y agoI don't know. I think the one of the best things about Angular for example is that you often see straight away what's happening because of it's declarative nature. With normal event handlers you don't know what's going on until you specifically search for the id/class/tag in all the JS files.
- egeozcan 13y agoIn that case, it's good to have some conventions but I agree that it takes a very small amount of carelessness for maintenance to become a PITA. I think using reactive templates which associate with a model is better than declaring every reaction on the DOM level though.
- deleted 12y ago[deleted]