Created
May 31, 2016 21:52
-
-
Save chiquitinxx/0f95fe6d3c4cee32298a832db9ce2391 to your computer and use it in GitHub Desktop.
Grooscript components and grails 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Todo List</title> | |
</head> | |
<body> | |
<div id="app"></div> | |
<asset:javascript src="grooscript-grails.js"/> | |
<asset:javascript src="jquery-2.2.0.min.js"/> | |
<asset:javascript src="webcomponents.min.js"/> | |
<grooscript:remoteModel domainClass="todo.Todo"/> | |
<grooscript:component src="component.MyTodo" name="my-todo-component"/> | |
<my-todo-component></my-todo-component> | |
<asset:deferredScripts/> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package component | |
import todo.Todo | |
class MyTodo { | |
List<Todo> todos | |
String newTodoTyped | |
static renderAfter = ['gotTodos', 'newTaskAdded'] | |
def gotTodos(newTodos) { | |
todos = newTodos | |
} | |
def checkedChanged(check) { | |
//Save the complete status: check.id + check.checked | |
} | |
def newTaskKey(element) { | |
newTodoTyped = element.value | |
} | |
def saveNewTask() { | |
if (newTodoTyped) { | |
new Todo(name: newTodoTyped).save().then { newTodo -> | |
//Saved in database | |
newTaskAdded(newTodo) | |
} | |
} | |
} | |
def newTaskAdded(newTodo) { | |
newTodoTyped = '' | |
todos << newTodo | |
} | |
def render() { | |
if (!todos) { | |
Todo.list().then { list -> | |
gotTodos list | |
} | |
} else { | |
p { | |
yield 'Insert new task:' | |
input(type: 'text', onkeyup: 'newTaskKey') | |
button(onclick: 'saveNewTask', 'Add task') | |
} | |
todos.each { todo -> | |
li { | |
input( | |
id: todo.id, | |
type: "checkbox", | |
onchange: 'checkedChanged', | |
checked: todo.completed ? 'false': 'true' | |
) { | |
yield todo.name | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package todo | |
import grails.rest.Resource | |
@Resource(uri='/todo', formats=['json']) | |
class Todo { | |
String name | |
boolean completed = false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment