Last active
August 29, 2015 14:16
-
-
Save enjalot/3ce69b8b4ba886c0fc35 to your computer and use it in GitHub Desktop.
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
.idea | |
node_modules | |
.DS_Store |
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
<Body:> | |
<h1>Hello, world!</h1> | |
<view is="sample"></view> | |
<sample:> | |
hey there!<br> | |
notebook id: {{notebook.id}}<br> | |
cells:<br> | |
<ul> | |
{{each cells as #cell,#i}} | |
<li> | |
cell {{#i}} {{#cell.id}} | |
</li> | |
{{/each}} | |
</ul> | |
<a on-click="addCell()">Add Cell</a> |
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
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) | |
page.render(); | |
}) | |
}) | |
}); | |
// define the controller for our sample component | |
function Component() {} | |
Component.prototype.init = function() { | |
var model = this.model; | |
var notebookId = model.scope("_page.notebookId").get() | |
model.set("notebookId", notebookId); | |
console.log("notebookId!", notebookId) | |
var filter = model.filter(model.scope("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("cells", filter); // reference the filtered cells in our local scope | |
} | |
//create only ever runs in the browser | |
Component.prototype.create = function() { | |
console.log("im in the browser only!") | |
} | |
// a view function available within the scope of our function | |
Component.prototype.addCell = function() { | |
var model = this.model; | |
var newCell = { | |
_notebook: model.get("notebookId") | |
} | |
var currentCell; | |
if(currentCell = model.get("currentCell")) { | |
newCell._prev = currentCell.id | |
} | |
cellId = model.scope("cells").add(newCell) | |
} | |
//associate the component's controller with the view | |
app.component("sample", Component) | |
// global view function | |
app.proto.test = function() { | |
console.log("test") | |
} |
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
{ | |
"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", | |
"derby": "^0.6.0-alpha35" | |
}, | |
"author": "", | |
"license": "MIT" | |
} |
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
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