Dynamic runtime objects.

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Dynamic runtime objects.

mkavanagh
Hi,

I am new to Grails and have spent some time trying to find an answer to this.

I have a taglib that I am using to do some processing for a web page.

I want to build an runtime data object that only will contain a connected string of files and other properties related to each file - possibly sets of files.

Once this is created this and I have put  all the relevant files into it then , in a separate place, I want to reuse it so that I can loop though the all files and properties and complete a task.

In terms of scope the first tag called from the html file for the web page creates the object.

Then the 2nd tag again called from the same html file uses the object.

Another way of looking at this is - I have used "def a = 10.0" to create a simple runtime data object - how do I extend this to one have linked objects and also to contain more than one datatype?  Also how do I manage the scope so that it can be seen across different unlinked tags?.

I want to avoid using a domain class as it is only needed within a runtime session.  

Thanks for you help in advance.


-Mike


 
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic runtime objects.

Nathan Wells
There are probably a couple ways to accomplish your goal. The first one that comes to mind is to create the object in your controller action and return it, a la:

def reusableObject = [
  declare: "a map",
  of: [
   depth: n
  ],
  withAnArray: [1, 2, 3],
]

return [reusableObject: reusableObject]

You can then use that object in your view and taglibs:

useObject.gsp:
<div>
  <my:taglibCall arg="${reusableObject}" />
</div>

UseObjectTaglib:

def taglibCall = { params, body ->
  def reusableObject = params.arg
  out << "depth = ${reusableObject.of.depth}"
  reusableObject.put("a", "new key")
} 

Since objects are passed by reference, you could then later (in the same view) call a taglib and reference "a" to get "new key".

At least, I think you should be able to. I haven't tested this out. Does that help?

Nathan Wells


On Tue, Nov 6, 2012 at 11:07 AM, mkavanagh <[hidden email]> wrote:
Hi,

I am new to Grails and have spent some time trying to find an answer to
this.

I have a taglib that I am using to do some processing for a web page.

I want to build an runtime data object that only will contain a connected
string of files and other properties related to each file - possibly sets of
files.

Once this is created this and I have put  all the relevant files into it
then , in a separate place, I want to reuse it so that I can loop though the
all files and properties and complete a task.

In terms of scope the first tag called from the html file for the web page
creates the object.

Then the 2nd tag again called from the same html file uses the object.

Another way of looking at this is - I have used "def a = 10.0" to create a
simple runtime data object - how do I extend this to one have linked objects
and also to contain more than one datatype?  Also how do I manage the scope
so that it can be seen across different unlinked tags?.

I want to avoid using a domain class as it is only needed within a runtime
session.

Thanks for you help in advance.


-Mike






--
View this message in context: http://grails.1312388.n4.nabble.com/Dynamic-runtime-objects-tp4637432.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Dynamic runtime objects.

Ian Roberts
In reply to this post by mkavanagh
On 06/11/2012 18:07, mkavanagh wrote:
> In terms of scope the first tag called from the html file for the web page
> creates the object.
>
> Then the 2nd tag again called from the same html file uses the object.
>
> Another way of looking at this is - I have used "def a = 10.0" to create a
> simple runtime data object - how do I extend this to one have linked objects
> and also to contain more than one datatype?  Also how do I manage the scope
> so that it can be seen across different unlinked tags?.

Within a taglib you have access to the pageScope, which is basically the
GSP model, the variables that are visible in the page.  You can assign a
value to pageScope.something in one tag and then fetch it back out in a
later tag.

def tag1 = { attrs, body ->
  pageScope.myMessage = "hello"
}

def tag2 = {attrs, body ->
  out << pageScope.myMessage
}

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Dynamic runtime objects.

mkavanagh
Hi Ian,

Thanks for helping me again - this looks like a neat way to do this.

How do I define and setup a runtime 'multiple' variable object, within a tag, holding more then one datatype as well as having multiple instances that can be linked together to form a sequence; namely first instance, second instance etc.

I appreciate that this is a basic question - if you give me some keywords I will investigate further.

Thanks again.


-Mike
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic runtime objects.

sbglasius
Hi Mike,

pageScope is just a map, so you can put anything in it

pageScope.myList = ['a','b']
pageScope.myMap = [a:'a', b:'b']
pageScope.myObject = someObject

etc...

/Søren


Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
Mobile: (+45)40449188, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


2012/11/7 mkavanagh <[hidden email]>
Hi Ian,

Thanks for helping me again - this looks like a neat way to do this.

How do I define and setup a runtime 'multiple' variable object, within a
tag, holding more then one datatype as well as having multiple instances
that can be linked together to form a sequence; namely first instance,
second instance etc.

I appreciate that this is a basic question - if you give me some keywords I
will investigate further.

Thanks again.


-Mike



--
View this message in context: http://grails.1312388.n4.nabble.com/Dynamic-runtime-objects-tp4637432p4637466.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Dynamic runtime objects.

mkavanagh
In reply to this post by mkavanagh
Hi,

pageScope works like a dream - thanks for all your advice.


-Mike