example /views/products/index.json.cfm

35 views
Skip to first unread message

dhga...@gmail.com

unread,
Jan 26, 2021, 11:07:45 AM1/26/21
to CFWheels

I have recently upgraded to cfwheels 2.2.0. I am attempting to upgrade my existing cfwheels app to add a new single-page-application that uses ajax/json / jquery (maybe angularjs) .

On https://guides.cfwheels.org/docs/responding-with-multiple-formats# , there is an Example: PDF Generation: "If you need to provide a PDF version of the product catalog, the view file at; /views/products/index.pdf.cfm may look something like this: ..."

1) Can someone provide me with a /views/users/index.json.cfm
that uses the same data structure as on;

2) Also please provide an example of a "index() " for my /controller/users.cfc.  Or does this look correct?

component extends="Controller" {
  function config() {
    provides("html,json");
  }
 
  function index() {
        //    Define the local scope.
        var LOCAL = {};

         // Prepare the message for the user.
        //    Create new API response
        LOCAL.AjaxResponse = {};
        LOCAL.AjaxResponse["success"] = true;
        LOCAL.AjaxResponse["errors"] = [];
        LOCAL.AjaxResponse["data"] = model("user").findAll(returnAs="structs");  

          
  // Respond to all requests with `renderWith()`.
  renderWith(data= LOCAL.AjaxResponse , hideDebugInformation=true);
  }
}

Thanks
Dan

Tom King

unread,
Feb 4, 2021, 12:45:02 PM2/4/21
to CFWheels
I perhaps wouldn''t use local in that context myself (but that's just me) - nothing stopping you though of course unless you want to access that scope in the view file itself.
Reply all
Reply to author
Forward
0 new messages