Accessing FORM and URL variables via $.event() from an admin-side plugin page in Mura

I have been learning a lot about Mura the last couple of weeks while building my first Mura plugin, but I have also had a lot of frustration because some things work differently when you are developing for a front-end page vs. a back-end (admin) page.

In the Mura Developer Documentation it states:

The Event scope simply wraps the current request's event object which contains merged data from both the CFML FORM and URL scopes.

If then goes on to say that the following code should return values from those scopes.


<cfset $.event('property') />

So if I have a URL variable like ?test=123, then this code should return the value '123':


<cfset $.event('test') />

This seems to work fine on pages I create for Mura display objects for the front end of the website, but for pages in the admin area of the site, this method only produces [empty string].

At first I decided that I would just then use FORM and URL normally (i.e FORM.test, URL.test), but then I had to go back to using to define all my variables, and I had to worry about what was in which scope, and it was just kind of a pain in the ass.

I searched and searched and search and I could not find ANY explanation about how this should work or how to make it work like I thought it should. Finally, someone at Blue River told me, on Twitter, that I should init() the MuraScope with the scope that I wanted in the config.cfm file. So I looked into this and it seems that I can do something like this to get part of the behavior I wanted:


<cfset form.siteID = session.siteID />
<cfset $ = application.serviceFactory.getBean('muraScope').init(FORM) />

You always need to pass in the siteID when you init the MuraScope, so I simply added it to the FORM struct and passed in the whole FORM. This seemed to get me PART of what I needed, but I wanted the URL struct in there too. I tried calling init() multiple times, but, as expected, that did not work. Finally, I found this post from Adam Presley on how to merge two structs.

Finally, I ended up doing this in my config.cfm for the plugin.



<cfif not isDefined("$")>
    <cfset initArgs = StructNew() />
    <cfset StructAppend(initArgs, URL) />
    <cfset StructAppend(initArgs, FORM) />
    <cfset initArgs.siteID = session.siteID />

    <cfset $ = application.serviceFactory.getBean('muraScope').init(initArgs) />
</cfif>

Keep in mind with this code that if the URL and FORM scopes each have a variable with the same name that the FORM scope's value will overwrite the URL's. This is what I wanted, because it is how the other frameworks I use work.

Now, I have NO IDEA, if this is the right way to do this. Like I said, I could not find any other way. If someone knows a better way or if they want to share with me why I should not do this, I would love to hear either way. But now I can use $.event('propertyName') on my admin pages without issue (so far).

Comments
Jas Panesar's Gravatar Hi Jason,

Handy solution here for anyone looking to do a plugin from scratch. Also an issue I found was that there's so many ways to do the same thing in Mura, and I didn't know what was the right, or best way to do it....

This issue was greatly simplified when I moved on to using the Mura FW/1 Base plugin instead of building a plugin from scratch..

The guys have done a lot of work to bake a lot of this into the Mura-FW/1 bread and most information is accessible easily through the rc (Request context), $ (mura scope), and any public/admin views you have to make work just fine.

If this is the same plugin I think you mentioned before, sounds like it's growing a bit.. :) Doing a plugin from scratch is a wonderful way to do learnt, but I'm not sure I'd have the yoda like patience you are having in figuring it all out, I just moved on to FW/1 plugin and everything works easier as long as I post a form to itself..

Jas
# Posted By Jas Panesar | 7/5/11 5:22 PM
Jason Dean's Gravatar @jas

I plan to do a FW/1 Plugin next, but really, this plugin is almost nothing. It is a simple Contact Us form with some dynamic content.

While I am sure I could do this with FW/1, I am doing this for the learning experience and because I am firmly of the belief that I should not need to use a framework to accomplish something simple, even if it is a simple framework.

I have built a FW/1 app before, so I do have some idea of what's involved and I feel like that is overkill for this plugin.

I do feel like the Mura docs need to be clearer about what works in which contexts. I wasted several hours trying to figure out what I was doing wrong when using $.event('property') in my views.
# Posted By Jason Dean | 7/5/11 5:41 PM
Matt Levine's Gravatar Thanks for pointing how there needs to be more clarity about where the values in the $.event() come from. I'll update the programmers guide to try an make it clearer. I'll also update the example config.cfm code in the programmers to use an initArgs struct that it populated by the Form and URL scope as your example shows.
# Posted By Matt Levine | 7/5/11 6:49 PM
Jason Dean's Gravatar @Matt,

Thanks for the confirmation that I am not completely mad (at least about this). I'm glad that, it seems, I am doing the right thing and that the docs can benefit from my struggles.

I'm curious, is there any reason that the URL and FORM scopes couldn't be added right in automatically like on the front end? Why must we take the extra step of adding it in the config.cfm?
# Posted By Jason Dean | 7/5/11 7:46 PM
Matt's Gravatar Fyi, the form and url scopes are now added automatically like on the frontend. It's been that way for a while partly in response to this thread, but I forgot to post back.

Thanks!
# Posted By Matt | 4/28/12 12:45 AM
Jason Dean's Gravatar Thx Matt. As usual, the response from Blue River is fantastic.
# Posted By Jason Dean | 6/20/12 11:28 AM
Adrian's Gravatar What would be nice is a cross table showing which scopes on http://www.getmura.com/blog/introducing-the-mura-s... are accessible in the different contexts. I've spent hours and still am trying to figure out how to access variables assigned in the fw/1 plugin inside the the content section in site manager.
# Posted By Adrian | 3/16/14 12:35 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1. Contact Blog Owner