Singletons are stored in application scope and hence the data stored in a singleton would be shared across all users. So this isn't what you want for a cart.
If you want to keep it simple, I'd suggest storing user data within your controller directly within session scope and dereferencing session scope in your view. So I think you'd want to use session.cart.??? for this rather than a bean.
That said, if you prefer to encapsulate the data for the cart within a bean, then when the user initiates interaction with the cart, I think you'd want something like this:
if ( ! structkeyexists( session, 'cart' ) {
session.cart = getBeanFactory().getBean('cart');
}
Just make sure from the docs that your getBean() call is creating a transient and not a singleton. There are options in there that I don't remember offhand. Keep in mind that once session.cart is created, whether it is a bean or data structure, you don't want to overwrite it, so you need to wrap the creation in a structkeyexists() call.
I generally do not use singletons in my views unless I'm rendering something. In my case, I use a singleton to render a translation of a term in multiple languages in the view, but that's it. If I needed data from a singleton, I'd obtain it in the controller, set it to an rc scoped variable, and then it would be available in the view, rather than passing a singleton into the view via rc scope.
Here's an example how I create singletons in my controllers using fw1's mechanism for injecting them.
variables.framework = {
....
diEngine = 'di1',
diLocations = 'services'
};
component accessors="true" {
property name="AppointmentService";
property name="ClientService";
property name="FinancialService";
property name="UtilsService";
property name="framework";
...
public void function listAppointments( struct rc ) {
rc.qClients= variables.clientService.findGroupedClientsForSelect();
rc.minDate = variables.appointmentService.findMinDate();
rc.maxDate = variables.appointmentService.findMaxDate();
...
To make things clear, appointment.cfc is in my services directory, so the convention is that services/appointment.cfc is located and injected as a property named AppointmentService. diLocations can be a list of directories.
Hope that helps.
Nando