Skip to content

Instantly share code, notes, and snippets.

@ile
Created August 28, 2013 01:07
Show Gist options
  • Save ile/6360991 to your computer and use it in GitHub Desktop.
Save ile/6360991 to your computer and use it in GitHub Desktop.
Racer bug?
app = require('derby').createApp(module)
.use(require 'derby-ui-boot')
.use(require '../../ui/index.coffee')
# ROUTES #
# Derby routes are rendered on the client and the server
app.get '/', (page) ->
page.render 'home'
app.enter '/list', (model) ->
model.on 'change', '_page.item_id', (id) ->
console.log 'change '+id
item_query = model.at 'items.' + id
model.subscribe item_query, (err) ->
if !err
item_query.ref '_page.item'
else
console.log err
app.get '/list', (page, model, params, next) ->
# This value is set on the server in the `createUserId` middleware
userId = model.get '_session.userId'
# Create a scoped model, which sets the base path for all model methods
user = model.at 'users.' + userId
# Create a mongo query that gets the current user's items
itemsQuery = model.query 'items', {userId}
# Get the inital data and subscribe to any updates
model.subscribe user, itemsQuery, (err) ->
return next err if err
# Create references that can be used in templates or controller methods
model.ref '_page.user', user
itemsQuery.ref '_page.items'
user.increment 'visits'
page.render 'list'
# CONTROLLER FUNCTIONS #
app.fn 'list.add', (e, el) ->
newItem = @model.del '_page.newItem'
return unless newItem
newItem.userId = @model.get '_session.userId'
@model.add 'items', newItem
app.fn 'list.remove', (e) ->
id = e.get ':item.id'
@model.del 'items.' + id
<Body:>
<div class="row">
<form class="span3" x-bind="submit: list.add">
<h3>Add item</h3>
<fieldset>
<label>Name</label>
<input type="text" value={_page.newItem.name}>
<label>Note</label>
<textarea>{_page.newItem.note}</textarea>
<div>
<button type="submit" class="btn">Add</button>
</div>
</fieldset>
<div class="well visits">
<b>Fun fact:</b> You've visited the list {_page.user.visits} times.
</div>
</form>
<div class="span8 offset1">
<table class="table">
<thead>
<tr>
<th>Item</th>
<th>Note</th>
</tr>
</thead>
<tbody>
{#each _page.items as :item}
<tr>
<td>{{:item.name}}</td>
<td>{{:item.note}}</td>
<td><button class="btn" x-bind="click: list.remove">Remove</button></td>
</tr>
{/}
</tbody>
</table>
<hr>
<select>
<option value="" selected="{equal(_page.item_id, '')}">select item</option>
{#each _page.items as :item}
<option value="{:item.id}" selected="{equal(_page.item_id, :item.id)}">{{:item.name}}</option>
{/}
</select>
<h3>selected item</h3>
<p>{_page.item}</p>
</div>
</div>
@ile
Copy link
Author

ile commented Aug 28, 2013

Modifications

index.coffee
line 12 ->

list.html
line 46 ->

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