Pages

Sunday, February 22, 2015

grails auto refresh a div with jquery

I guess the toughest dead end i had so far - i needed to refresh only a part of a page, one div, every 10 seconds, for example. I had completely no idea how to solve it. After some time and no responses on SO i thought that i probably needed to create a separate view and point the div to this view with jquery, something like that. 
The solution turned out to be almost like i thought.
I've solved this with help of joshm76 from #grails on freenode.
There are several steps for this solution:
  1. i need a template with the div contents i need to update
  2. i need a controller that with a model passes needed values to the template
  3. i need to include this inside the div i want to update with a 
  4. with jquery i update the div contents
1) Template is a .gsp file with _ before the name, like _myTemplate.gsp. Put it in the view folder of the controller you will be using.
2)
    def action(){
    def var
    //some logic to populate the variable

    Map model = [myVar: var]
    render(template: '_myTemplate', model: model)
}
in the template the variable can be accessed with ${myVar}
3) inside the div you need to update, put the <g:include> instead of the content you had there before (now the content is in the template)
<g:include controller='controller' action='action' />
4) the jquery part (fetches the template every 5 seconds):
    $(document).ready(
            function() {
                setInterval(function() { 
                $('#divId').load('/app/controller/action');
                }, 5000);
            });
you can also use the $.get() function, the result seems to be identical

Sunday, February 15, 2015

Grails debugging

Debugging with grails is extremely easy.

1) Run your app with grails --debug-fork run-app
2) Wait for it to accept connections
3) Remote debug on localhost and port 5005

Monday, February 9, 2015

grails gsp form vs g:form

I had this problem recently. I have several forms with submit buttons, that call different controllers and actions. And i used simple <form> tags. The submit buttons were calling completely different controller that was specified in the <form controller=''>. Well, turns out that to user controller/action parameters i have to use the grails <g:form> tag. Almost broke my brain on this.

Queit

My current pet project is Queit, a web service for creating queues. I had to begin from scratch, because my approach before i read a few books on grails was completely wrong. I started on around 22 January and now the main functional is working. Just some fixes there and here, a decent design and probably ready to launch.

And then like BOOM out of nowhere - my friend just launched another startup. An anonymous messenger. He has a lot of resources, but made this mobile app completely on his own. And i am sure that he will reach his milestone - 200 000 users.

I am insanely motivated!

Saturday, February 7, 2015

Elon Musk never gives up.

After 3rd unsuccessful launch of his rocket Elon faced a decision: quit, or invest everything he had left in the fourth attempt.






Wednesday, February 4, 2015

Grails bad practice #1: storing domain objects in session variable

I've learnt that storing domain objects in a session variable is a bad practice. I had to refactor some code, and now everything works correctly. Where were problems when i needed to access some domain properties inside a gsp, but those were solved by passing the needed parameters, not the whole domain classes.

I had a controller and a corresponding gsp code block:

def user = session?.user
    def queue = Queue.findById(params.queueId)

    if(queue){
        user.removeFromQueues(queue)
        queue.delete(flush:true)

        flash.message = "deleted queue with id: ${queue.id}"
        redirect(controller:'queue', action:'index')
    }
<g:each in="${session?.user?.queues}">
        <div id="queue">
            ${it.name} id:${it.id}
                <div id='queueButtons'>
                    <g:hiddenField name="queueId" value="${it.id}" /> 
                    <g:link controller='queue' action='delete' params="[queueId: "${it.id}"]">X</g:link>
                </div>
        </div>
    </g:each>
Now i dont save user in session, i save userId in session, and when needed fetch the user in controllers. On gsp where i needed the user domain to get queues, i passed session.queues that was set in a controller after a user fetch session.queues = user.queues.

Tuesday, February 3, 2015

racetrack and collab-doto public repositories

Hi!

Sharing my completed racetrack and collab-todo apps from "Beginning Grails, Groovy and Griffon" and "Getting started with Grails"

racetrack @ BitBucket

collab-todo @ BitBucket