Skip to main content

Posts

Showing posts with the label rails

My new way of using PHP Traits & Interfaces

A popular feature in Rails called "mixins" came to PHP a while back under the name "traits".  Traits let library developers give you a lot of functionality with little to no effort by injecting code into your classes that runs as if originally authored in them. Just toss a line of code into the class you want to augment and you're off to the races. The one snag I found when using traits however is that they don't participate in any kind of type system.  If desperate enough, you can use reflection to pull out whether a class has one applied or not, but that's only half the battle and a lot of work each time. This puts me in a slight bind when I'm interacting with classes using a trait I've made as I have no way to type check or hint them in. But then it dawned on me... interface MyInterface { public function thisMethodIsImportant(); } trait MyInterfaceImpl { public function thisMethodIsImportant() { return "Thanks...

Your Server-Side Form Library

If it isn't already obvious from my previous posts, I've been spending a lot of time on the client side of things with JavaScript and the Dojo toolkit. I had a chance last weekend to show off a little bit of Dojo and once I got past the lightweight technique for bootstrapping it from the server , my demonstration suddenly got a lot less glamorous. I think part of it got lost amidst what resembled gruelling swing form code.  I was lucky in that I already had some completed forms to show off, so it was possible to show the kind of experiences you can deliver when you use a good client-side framework. There was however a takeaway for me which after a lot of thinking, I think I've managed to figure out... It's Holding you Back! Your server side form library is killing your UX, coupling your model to the view and tying down your options client-side. I've worked with forms in Symfony 1.4 extensively and have become quite adept at bending them to whatever need ma...

Dojo and Rails CSRF

If you're using Dojo and Rails & are having some issues with asyncs to your server being rejected, consider employing the following in your xhr or JsonRest : headers: { "X-CSRF-Token": query("meta[name='csrf-token']").attr("content") } In my particular situation, I was trying to do HTTP POSTs and Rails was terminating my session likely because it perceived the lack of a CSRF token as a hijack attempt.  The code here simply adds the token as an HTTP header in the request. I think once I've had some time with this fix, I might look at subclassing these objects for my project and pre-populating the header.  Then I'll simply substitute my implementations for the default Dojo ones. Which - come to think of it - would probably make for a good case when discussing the advantages of AMD . Fear not, if you happen to be using jQuery and are experiencing this issue, here's an equivalent solution .