stevewithington / MuraFW1

Base Mura CMS plugin using FW/1 as its application framework.
Apache License 2.0
27 stars 23 forks source link

Calling model from event, e.g. onBeforePageSubTypeSave #42

Closed fraxen closed 8 years ago

fraxen commented 8 years ago

Hmmm...

I am trying to introduce some logic in the eventhandler.cfc for an event onBeforePage{SubType}Save.

Ideally I would like to call the model directly, and I am testing different things when editing an item through the site manager/admin interface (this is Lucee 4.5 on Windows, with Mura 6.2)

When the event body is: getApplication().getBeanFactory().getBean('{MyService}') it fails with a key [SUBSYSTEMS] doesn't exist error - I guess this is because the application has not been set up/loaded through the request parameters.

When I try with a getApplication().doAction('mysubsystem:events.onBeforePageMysubtypeSave') (and I have set up that subsystem, controller etc properly, with a blank view) then I get an error Unable to find a view for 'frontend:carch.list' action. Type: FW1.viewNotFound '/muraWRM/admin/frontend/views/carch/list.cfm' does not exist. - I think the action/view etc conflicts with the admin application.

The workaround is to (new framework.ioc('/#variables.framework.package#/model').getBean('{MyService}')), but then I have to implement a hack to get my constants passed in properly, both here and in setupApplication() in Application.cfc...

Is there a better way?

fraxen commented 8 years ago

How I set this up - I set up my constants in fw1config.cfm by setting the variables.framework.diConfig variable. Then I edited the bean factory setup (line 102 in Application.cfc) to pass that in - that should probably be used there.

Then I added this helper method in eventHandler.cfc:

    private any function getModel() {
        return new framework.ioc('/#variables.framework.package#/model', framework.diConfig);
    }

And now I can have events call getModel().getBean('MyService').myMethod() etc... !

fraxen commented 8 years ago

further improved: includes/fw1config.cfm Added towards the bottom:

variables.framework.beanFactory = function() {
  return new framework.ioc(
    '/#variables.framework.package#/app2/model,/#variables.framework.package#/app3/model'),
    variables.framework.diConfig
  );
};

Application.cfc Modified line 102 to: local.beanFactory = variables.framework.beanFactory();

includes/eventHandler.cfc to add to the top, after the include:

property name='model';
setModel(variables.framework.beanFactory());

Now any getModel() calls in the eventHandler returns the full model, it is only specified once, and the bean factory is cached in the eventHandler for the life of the application!

fraxen commented 8 years ago

@stevewithington should I provide a PR for this?

fraxen commented 8 years ago

The proposed changes above have the downside that di1 is instantiated twice, both in Application.cfc and in eventHandler.cfc - now I found the best answer - you can reach the bean factory through: application[variables.framework.applicationKey].factory

stevewithington commented 8 years ago

@fraxen i've just updated this plugin for Mura v7, and am using Mura's own di1 bean factory. hopefully this should just work now as it does in the examples.