1

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

shehata
  • 531
  • 6
  • 20

1 Answers1

2

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.

Kyle Finley
  • 11,842
  • 6
  • 43
  • 64
  • 1
    To explain this a bit more, `books` is a collection, and collections are represented as objects in the model. In Derby templates, `{{each}}` blocks can only iterate over arrays and *not* over objects. As Kyle pointed out, you can use a refList to create an array of documents in a collection corresponding to query results. Alternatively, you can use model.filter to map a collection into an array. – Nate Smith Dec 11 '14 at 17:24
  • what i would like to ask, i always need to use subscribe before i use model.get() ? because the first method in the answer didn't work for me, but the second works fine. – shehata Dec 12 '14 at 02:52
  • 1
    Yes, you have to use subscribe to populate the model. Thanks for let me know about the error, I removed it so that others won't be confused. – Kyle Finley Dec 12 '14 at 03:14