web-servicescoldfusionframeworkscoldfusion-11cfc

Difference between calling .cfm and .cfc with JQuery Ajax?


I'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.


Solution

  • To long for a comment

    I agree with what the others have already said. There is no specific answer because it always depends on more things than just this bit of code. Having said that...

    I found this from the Adobe documentation here which seems relevant. Below is an excerpt from that documentation. I highlighted in bold the part which seems relevant. Notice the part about after the CFC is instantiated. You can read more at the link.

    When to use CFCs

    You can use CFCs in the following ways:

    • Developing structured, reusable code
    • Creating web services
    • Creating Flash Remoting elements
    • Using asynchronous CFCs

    Developing structured, reusable code

    CFCs provide an excellent method for developing structured applications that separate display elements from logical elements and encapsulate database queries. You can use CFCs to create application functionality that you (and others) can reuse wherever needed, like user-defined functions (UDFs) and custom tags. If you want to modify, add, or remove component functionality, you make changes in only one component file. CFCs have several advantages over UDFs and custom tags. These advantages, which CFCs automatically provide, include all of the following:

    • The ability to group related methods into a single component, and to group related components into a package
    • Properties that multiple methods can share
    • The This scope, a component-specific scope
    • Inheritance of component methods and properties from a base component, including the use of the Super keyword
    • Access control
    • Introspection for CFC methods, properties, and metadata
      CFCs have one characteristic that prevents them from being the automatic choice for all code reuse. It takes relatively more processing time to instantiate a CFC than to process a custom tag. In turn, it takes substantially more time to process a custom tag than to execute a user-defined function (UDF). However, after a CFC is instantiated, calling a CFC method has about the same processing overhead as an equivalent UDF. As a result, do not use CFCs in place of independent, single-purpose custom tags or UDFs. Instead, use CFCs to create bodies of related methods, particularly methods that share properties. For more information about UDFs, custom tags, and other ColdFusion code reuse techniques, see Creating ColdFusion Elements.

    Creating web services

    ColdFusion can automatically publish CFC methods as web services. To publish a CFC method as a web service, you specify the access="remote" attribute in the method's cffunction tag. ColdFusion generates all the required Web Services Description Language (WSDL) code and exports the CFC methods. For more information on creating web services in ColdFusion, see Using Web Services.

    Now I don't always trust the Adobe documentation as they have a nasty habit of just carrying forward the existing documentation from version to version. So who knows when this was originally written and if it is still true. And this is specific to Adobe's ColdFusion, Lucee is likely better at handling this but I'm not sure.

    That document also refers to this document - Selecting among ColdFusion code reuse methods. I will include that info here as well.

    The following table lists common reasons to employ code reuse methods and indicates the techniques to consider for each purpose. The letter P indicates that the method is preferred. (There can be more than one preferred method.) The letter A means that the method provides an alternative that is useful in some circumstances. This table does not include CFX tags. You use CFX tags only when it is best to code your functionality in C++ or Java. For more information about using CFX tags, see Using CFX tags.

    enter image description here