chiquitinxx / grooscript-plugins

Grooscript plugins - Gradle and Grails 3
12 stars 1 forks source link

JavaScript exception: Uncaught TypeError: result.add is not a function #5

Closed tepsl closed 8 years ago

tepsl commented 8 years ago

In grooscript-grails.js, an exception occurs because result.add is undefined when ob == 'remove'.

    //Convert a javascript object to 'groovy', if you define groovy type, will use it, and not a map
    gs.toGroovy = function(obj, objClass) {
        var result;
        if (obj && typeof(obj) !== "function") {
            if (obj instanceof Array) {
                result = gs.list([]);
                var i;
                for (i = 0; i < obj.length; i++) {
                    result.add(gs.toGroovy(obj[i], objClass));
                }
            } else {
                if (obj instanceof Object) {
                    var ob;
                    if (objClass) {
                        result = objClass();
                        for (ob in obj) {
                            result[ob] = gs.toGroovy(obj[ob]);
                        }
                    } else {
                        result = gs.map();
                        for (ob in obj) {
                            // here
                            result.add(ob, gs.toGroovy(obj[ob]));
                        }
                    }
                } else {
                    result = obj;
                }
            }
        }
        return result;
    };
chiquitinxx commented 8 years ago

Hello! Thank you for report it. I need to reproduce to fix it, can you share an example please?

I have tried gs.toGroovy({remove: 'remove'}) and gs.toGroovy('remove') without success.

tepsl commented 8 years ago

Sorry for late reply. My code is almost based on http://grooscript.org/grails3-plugin/ The grails version is 3.0.9.

Add dependencies:

dependencies {
    ...
    compile "org.grails.plugins:grooscript:1.1.2"
    runtime "org.grails.plugins:jquery:1.11.1"
    ...
}

Add domain class:

package grooscript_sample

import grails.rest.Resource

@Resource(uri='/books')
class Book {
    String title
    String author
    static constraints = {
        author nullable: true
    }
}

Edit Bootstrap.groovy:

import grooscript_sample.Book

class BootStrap {

    def init = { servletContext ->
        new Book(title:'First Book').save()
        new Book(title:'Second Book', author:'some author').save()
    }
    def destroy = {
    }
}

Edit view/index.gsp:

<!DOCTYPE html>
<html>
<head>
    <title>Rest grooscript demo</title>
    <asset:javascript src="jquery-2.1.3.js"/>
    <asset:javascript src="grooscript-grails.js"/>
</head>
<body>
<grooscript:remoteModel domainClass="Book"/>
<h2>List of books</h2>
<div id="bookList"/>

<grooscript:code>
import grooscript_sample.Book

def drawBooks = {
    Book.list().then { list ->
        $('#bookList').html ''
        list.each {
            $('#bookList').append('<p>Title: '+it.title+' - Author: '+ (it.author?: 'unknown') +'</p>')
        }
    }
}

$(document).ready {
    drawBooks()
}
</grooscript:code>

<asset:deferredScripts/>
</body>
</html>

Then start grails and open http://localhost:8080/ No book is listed and JavaScript error is shown in developer console.

chiquitinxx commented 8 years ago

I got it, the problem is the documentation. Not enought clear, that you have to use JSON and not xml. The client expects json from the server. I'm going to update the documentation.

@Resource(uri='/books', formats=['json'])

Thank you very much for report it :)

tepsl commented 8 years ago

Got it working! Thanks.

chiquitinxx commented 8 years ago

Cheers thank you! In time for the new release 1.1.3