Search code examples
javascriptnode.jsderbyjsracerjsisomorphic-javascript

couldn't show list of documents in the view using derbyjs


It's my first time with derbyjs, don't know if i am being stupid or is it a lack of documentation. i have a model called "books", and i am just trying to show list of books.

here is my code:

module.exports = {
    properties: {
        title: {type: 'string', minLength: 6},
        author: {type: 'integer', minimum: 0},
        image: {type: 'string'},
        status: {type: 'integer', minimum: 0, maximum: 1}, // 1 read, 0 wants to read
        comment: {type: 'string'}
    },
    required: ['title']
}

and the schema list

module.exports = {
    schemas: {
        auths: require('./model/auths'),
        products: require('./model/products'),
        books: require('./model/books')
    }
}

the index js

app.get('/shelf', function(page, model, params, next){
    model.subscribe('books', function(){
        var book = model.at('books.669374b5-8470-4f3a-a25f-0995a5a92a7a');
        model.ref('_page.book', book);
        page.render('home');
    });
});

i expect to have "books" in the view, so i wrote {{each}} like this

{{ each books as #b}}
    {{ #b.title }}
{{/each}}

but nothing shows up, although this works fine and render as expected

{{ _page.book.title }}

also at the web console, this works fine and shows 3 books

app.model.get('books')

notice: i added the books through the web console, something like this

app.model.add('books', {title: 'something'})

inside the subscribe function, i have tried to

var books = model.get('books');
model.ref('_page.books', books);

but an error rise up

any idea what i am doing wrong ? i really like derbyjs but this is holding me back for few days


Solution

  • If you would like for the page to update automatically you should use a ref instead of a get() however, e.g.

    app.get('/shelf', function(page, model, params, next) {
      var booksQuery = model.query('books', {});
      model.subscribe(booksQuery, function(err) {
        booksQuery.ref('_page.books');
        page.render('books');
      });
    });
    

    Template:

    {{ each _page.books as #b}}
      {{ #b.title }}
    {{/each}
    

    In addition to the official Derby docs I've found the derby-faq to be a good resource.