Difference between calling .cfm and .cfc with JQuery Ajax?
Copy link to clipboard
Copied
'm working on the new system and there are few things that are different from what I used to see. Basically there is an JQuery ajax call with "POST" type and url pointing to .cfm
page. The .cfm
page will return html
table.
After talking to lead developer he mentioned that this method is more efficient. This way calling .cfm
we do not create new instance each time we make a call. The other way if we use .cfc and call a function new instance will be created each time. I do not know everything behind the screen and deep layers of ColdFusion.
One other thing he mentioned this way it's better since we do not use any frameworks. I have been working with ColdFusion for the past 4 years and what I seen in the past is JQuery Ajax calling component.cfc
with specific method
name. The data is returned and table is built dynamically. I was wondering if someone knows more about this and why the .cfm
might be better than calling .cfc
.
Thank you.
Copy link to clipboard
Copied
It depends. CFM is the better choice for static, simple or one-off presentation pages. CFC is the better choice for dynamic, input-dependent, reusable, business logic. Therefore what you describe at the end of the last paragraph suits a CFC better.
Copy link to clipboard
Copied
For anyone else who comes across this question, there was some very good discussion on Stack Overflow about this question.
Out of curiosity, what was the ultimate solution?
Copy link to clipboard
Copied
I'm not sure the statement made by "lead developer" in the second paragraph is accurate. If you have a CFC that has methods with `access=remote`, the difference in performance of making requests to it vs. to a CFM file is negligible. Yes, a new instance of the CFC is made each time; but for CFCs that don't involve storing state information, they are not much slower than calling CFMs.
Also, in a properly architected application, where business logic lives in the model and is managed by a Dependency Injection (DI) framework, the functions in the remote-access CFCs would just be simple wrappers that call down to the model. Services in the model would likely be cached as "singletons" by the DI framework. Thus the remote-access CFCs would be very small and quickly instantiated on demand.
Assuming no MVC framework is being used, I would lean toward using remote-access CFCs over CFM files.
Copy link to clipboard
Copied
I've been working with CF for twenty years or more, and I can't count all the times I've heard or seen "lead" developers worry way too much about things that don't really mean that much, instead of worrying about the things that do matter (for example, database interaction efficiency). This to me is one of those things. Yes, there is overhead in instantiating a CFC over and over again. But that overhead is (a) pretty small, and (b) easy to avoid in many (but not all) cases by caching a singleton as described above - and you don't need an MVC framework or a DI framework to do that. You can just ... do it yourself directly.
Now, to me, the more interesting issue is, does this generate and return HTML. CFM is very good for doing that. CFC is not quite as good (but probably good enough in most cases). If you find you're generating significant amounts of HTML you're probably better off using CFMs for that. If you're generating pretty small snippets, it doesn't matter what you use.
Ultimately, the real decision you have to make is, which file is easier to maintain for you and your developers? That's what ColdFusion is really all about. If all you cared about was performance, you'd use ... something else. So, which is easier for you to understand, and explain to your team, and which takes less time when someone asks for a change in it?
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
I would also point out again that if this is using Datatables, I'd use a CFC to return nothing more than JSON and make Datatables worry with the display. That's what Datatables is for.

