📜 ⬆️ ⬇️

Derby.js TODO or not TODO



Some programmers have already realized the emerging prospects and took up the study of Derby. Others are watching with interest what is happening, but are not ready at the moment to part with their favorite framework for various reasons. Still others miss Derby-tutorials, believing that this will somehow stop the progress and the oncoming wave of full-stack frameworks.

Well, our locomotive is gaining momentum and today we dive into our Derby-research.
')



Set up the environment and create a bare-project .

/lib/app/index.js

app.get('/', function(page, model) { model.subscribe('todos', function(err) { if (!model.get('todos')) { model.add('todos', {text: 'Todo 1'}); model.add('todos', {text: 'Todo 2'}); } page.render(); }) }); 


Here we subscribed to the entire todos collection. In the future, we will do all our data manipulations exactly after we subscribed to them, because prior to subscribe, our model is empty. Now add a couple of objects there, if there is no such collection.
model.add is a wrapper over model.set. The only thing she does is generate the id itself. We could write this:

 var id = model.id(); //  guid   require('node-uuid').v4() model.set('todos.' + id, {text: 'Todo 1'}); 


Our collection is a js object. If we make model.get('todos') , we get:

 { "e1b8075c-de9a-458a-aa3c-e9b383691521": { "text":"Todo 1", "id":"e1b8075c-de9a-458a-aa3c-e9b383691521" }, "26cd5f4a-c503-4c25-aeeb-a28c8c034d08": { "text":"Todo 2", "id":"26cd5f4a-c503-4c25-aeeb-a28c8c034d08" } } 


This is good if we want to get objects out of it by going:

 var todo = model.get('todos.e1b8075c-de9a-458a-aa3c-e9b383691521'); 


But if we want to display our todos in html, then we would be better off an array. Meet, we have a wonderful thing - filter:

 app.get('/', function(page, model) { model.subscribe('todos', function(err) { if (!model.get('todos')) { model.add('todos', {text: 'Todo 1'}); model.add('todos', {text: 'Todo 2'}); } var filter = model.filter('todos'); filter.ref('_page.todos'); page.render(); }) }); 


/views/app/index.html

 <Body:> {#each _page.todos as :todo} <p>{:todo.text}</p> {/} 


Here we create a filter for todos. It dynamically monitors changes to this collection and displays the result in '_page.todos' with the help of refList, about which a bit later. But the filter would not be a filter if he did not know how to filter. We can do something like this:

 var todos = model.filter('todos', function(todo) { return todo.text == 'Todo1'; }).get(); 


Here we immediately retrieved the filtered array.

 var todos = model.filter('todos').sort('text').get(); 


And here we also did the sorting by the text field.
Neither filter nor sort know anything about your database. They operate only with data that is in the model. Complete the model before it!

So there with refList? This is the so-called references. Allow to bind data between two paths. They are rarely used directly, but they are used, for example, filter and queries.

 app.get('/', function(page, model) { model.subscribe('todos', function(err) { if (!model.get('todos')) { model.add('todos', {text: 'Todo 1'}); model.add('todos', {text: 'Todo 2'}); } var ids = Object.keys(model.get('todos')); model.set('_page.ids', ids) model.refList('_page.todos', 'todos', '_page.ids'); page.render(); }) }); 


ids is a list of IDs of those todo that we want to receive as a result. They also set the order in the array '_page.todos'. We can change the '_page.ids' and this will immediately affect the '_page.todos'.

Let's torture subscribe:

 model.subscribe('todos', function(err) { //     todos }); model.subscribe('todos.e1b8075c-de9a-458a-aa3c-e9b383691521', function(err) { //     }); model.subscribe('todos.e1b8075c-de9a-458a-aa3c-e9b383691521.text', function(err) { //       }); model.subscribe('users', 'todos.e1b8075c-de9a-458a-aa3c-e9b383691521.text', function(err) { //   ,     }); 


Suppose we are a very busy person and we have a million todos. And we want to subscribe only to those whose text contains certain characters. Why do we need the entire collection on the client? paths are useless here. filter too. Queries come to our rescue:

 app.get('/', function(page, model) { var query = model.query('todos', {text: 'Todo 1'}) model.subscribe(query, function(err) { if (!model.get('todos')) { model.add('todos', {text: 'Todo 1'}); model.add('todos', {text: 'Todo 2'}); } query.ref('_page.todos'); page.render(); }) }); 


{text: 'Todo 1'} is Mongo Queries . That is, the livedb-mongo adapter forwards this object directly to Mongo. For other databases, you can write your adapters and do it somehow differently.

Derby.js materials

Source: https://habr.com/ru/post/196088/


All Articles