Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created February 28, 2015 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/d4f604f24b893ec90724 to your computer and use it in GitHub Desktop.
Save enjalot/d4f604f24b893ec90724 to your computer and use it in GitHub Desktop.
.idea
node_modules
.DS_Store
<Body:>
<h1>Hello, world!</h1>
hey there!<br>
notebook id: {{_page.notebookId}}<br>
cells:<br>
<ul>
{{each _page.cells as #cell,#i}}
<li>
cell {{#i}} {{#cell.id}}
</li>
{{/each}}
</ul>
<a on-click="addCell()">Add Cell</a>
var derby = require('derby');
var app = module.exports = derby.createApp('notebook', __filename);
app.use(require('derby-debug'));
app.loadViews(__dirname);
app.get('/', function(page, model) {
notebookQuery = model.query("notebooks", {name: "foo"})
notebookQuery.fetch(function(err) {
notebooks = notebookQuery.get()
var notebookId;
if(!notebooks.length) {
notebookId = model.add("notebooks", {name: "foo"})
} else {
notebookId = notebooks[0].id
console.log("already there", notebookId)
}
var notebook = model.at("notebooks" + notebookId)
var cells = model.query("cells", {_notebook: notebookId})
model.subscribe(notebook, cells, function(err) {
model.set("_page.notebookId", notebookId)
var filter = model.filter("cells", // model.scope gives you access to the root scope
function(c){ return c._notebook === notebookId })
.sort(function(a,b) { if(b._prev === a.id) return -1; return 1;})
model.ref("_page.cells", filter); // reference the filtered cells in our local scope
model.ref("_page.notebook", notebook)
page.render();
})
})
});
// global view function
app.proto.addCell = function() {
var model = this.model;
var notebookId = model.get("_page.notebookId")
var newCell = { _notebook: notebookId }
var cellId = model.add("cells", newCell)
}
{
"name": "notebook",
"version": "1.0.0",
"description": "Mini derby-app",
"main": "index.js",
"scripts": {
"start": "node server.js"
},
"keywords": [
"derby", "mini"
],
"dependencies": {
"derby-starter": "^0.2.6",
"derby-debug": "^0.1.0"
},
"author": "",
"license": "MIT"
}
require('derby-starter').run(__dirname, {port: 3000});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment