Skip to content

Instantly share code, notes, and snippets.

@raila
Created August 28, 2011 00:32
Show Gist options
  • Save raila/1176064 to your computer and use it in GitHub Desktop.
Save raila/1176064 to your computer and use it in GitHub Desktop.
batman keypath test
# Create our application and namespace.
class Alfred extends Batman.App
@global yes
# setup our root route. When the app starts up, it will automatically call TodosController::index
@root 'todos#index'
# Define the principal Todo model with `body` and `isDone` attributes, and tell it to persist itself using Local Storage.
class Alfred.Todo extends Batman.Model
# @global exposes this class on the global object, so you can access `Todo` directly.
@global yes
# @persist tells a model which storage mechanism it should use to load and save. Batman.LocalStorage is the simplest, simply persisting your records to the browser storage.
@persist Batman.LocalStorage
# @encode tells the persistence mechanism which fields to archive. You can also setup specific encoder and decoder functions to handle specific types of data.
@encode 'body', 'isDone', 'bank'#, 'bank.name'
@validate 'body', presence: yes
# just some defaults, but these are optional
body: ''
bank: {}
isDone: false
class Alfred.TodosController extends Batman.Controller
emptyTodo: null
index: ->
@set 'emptyTodo', new Todo
# add some example todos to show off.
Todo.load (error, todos) ->
# you always want to make sure you handle errors (more elegantly than this) when writing connection code
throw error if error
if not todos.length
callback = (error) -> throw error if error
new Todo(body: 'joker escaped arkham again', isDone: true).save(callback)
new Todo(body: 'riddler sent riemann hypothesis', bank: {name: "NASPA", no: "111111"}).save(callback)
new Todo(body: 'bane wants to meet, not worried').save(callback)
# return false to prevent the implicit render of views/todos/index.html
return false
create: =>
@emptyTodo.save (error, record) =>
throw error if error
console.log "record", record
# record only shows:
# _batman: _Batman
# body: "111"
# destroy: function (){return fn.apply(me,arguments)}
# dirtyKeys: Hash
# errors: ErrorsHash
# id: 35
# load: function (){return fn.apply(me,arguments)}
# save: function (){return fn.apply(me,arguments)}
# __proto__: ctor
# bank: {name: ""} ## doesnt show up!
# is this possible with keypaths?
# input(data-bind="todo.bank.name")
# we use set so that our form will automatically update with the new Todo instance
@set 'emptyTodo', new Todo
return false
new: =>
console.log "new"#, Todo.toJSON()
callback = (error) -> throw error if error
new Todo(body: 'test', bank: {name: "NASPA", no: "111111"}).save(callback)
Alfred.run()
<div id="batman" class="view">
<form data-formfor-todo="controllers.todos.emptyTodo">
<input placeholder="add a todo item" data-bind="todo.body" class="new-item">
<input placeholder="add a bank name" data-bind="todo.bank.name" class="new-item">
<button data-event-click="controllers.todos.create">speichern</button>
</form>
<ul id="items">
<li data-mixin="animation">
<input type="checkbox" data-bind="todo.isDone" data-event-change="todo.save">
<span data-bind="todo.body" data-addclass-done="todo.isDone" data-mixin="editable" class=" done"></span>
<span data-bind="todo.bank.name"></span>
<a data-event-click="todo.destroy" href="#">delete</a>
</li>
<button data-event-click="controllers.todos.new">new TODO</button>
</div>
@raila
Copy link
Author

raila commented Aug 28, 2011

My problem is described in the create function!
Is this even possible to use keypaths like this on input fields?
input(data-bind="todo.bank.name")

I know that Model assosciations are still missing. Maybe I just have to wait.
http://nciagra.github.com/batman.js-presentation/#40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment