Dmitriy Smirnov
Dmitriy Smirnov

Reputation: 449

Why use CakePHP's JsHelper?

I'm just starting with CakePHP and was wondering if someone could explain the true benefit of using its JsHelper over coding regular static jQuery and JS. So far, I don't really see how the helper would make creating scripts easier or faster.

Upvotes: 8

Views: 1206

Answers (3)

sibidiba
sibidiba

Reputation: 6360

JsHelper is like scaffolding: comes very handy if you need just the basic stuff, and only the basic stuff. That is, ajaxify a pagination or some elements.

But beyond that, it is better to write your own code, because you will need things as you need them, and not how the helper is aligned by default.

As everything else in a framework: it is a tool. Check your requirements and use the best tools available.

Upvotes: 0

mark
mark

Reputation: 21743

for the same reason I wrote my GoogleMaps Helper ;) the basic idea is that you can use the same language (php in this case) as the rest of the application and you can pass in any php option arrays and arrays holding data values and the helper is supposed to cake care of it.

it is similar to cakephp as a wrapper for php. it wraps your code and can help keep it dry.

don't get my wrong - i never ever used the js/ajax helper myself. but I can understand why some want to chose it over writing JS themselves. in some cases the output can even be more "correct" (if you don't know about the potential problems). for example the IE bug: if you output {} options yourself and forget to remove the last , it will not run in IE6 etc. that can't happen with the helpers as wrapper - at least it shoudnt ;)

so with the helper it either doesnt run at all or runs as a team of skilled developers designed it to work. especially for not so skilled developers this is usually a win-win situation: fast and more reliable. you can always start to switch to manual stuff later on (if you see the actual JS output and start to understand it).

also - when any of the js methods need to change for some reason your way of using the helper usually doesn't. if you don't use the abstraction you might find yourself in the need to manually adjust all occurrences.

Upvotes: 4

Oldskool
Oldskool

Reputation: 34867

As like any Helper, the JsHelper is a way to parse stuff into your view in a simplified way. But putting in "raw" JS/jQuery code into your view would work just fine by using $this->Html->scriptBlock for example.

There is not a real benefit I could think of other than if jQuery would ever change the syntax of a specific function, you wouldn't need to adjust your code when using the JsHelper, since the core then needs that update to be implemented, rather than you having to change all your "raw" JS code throughout all of your views.

Upvotes: 0

Related Questions