These are chat archives for canjs/canjs

9th
Jun 2016
Sunil George
@georgesunil81
Jun 09 2016 18:33
A quick question, is there any elegant way of suppressing/filtering out some records from a model being passed into the template scope. Is there anything that I can do at the model level to achieve this filtering based on some custom condition(s). Could you link me to some example?
Kevin Phillips
@phillipskevin
Jun 09 2016 18:34
why do you want to do that?
Sunil George
@georgesunil81
Jun 09 2016 18:36
@phillipskevin My current model retrieves data records based on certain status, but I want to apply further filtering based on dates on it (I dont want to modify the underlying REST API but wanted to handle that on the client).
Kevin Phillips
@phillipskevin
Jun 09 2016 18:37
I would just make a derived property on the viewmodel
Sunil George
@georgesunil81
Jun 09 2016 18:37
I could iterate through all records in my controller and do it, but it will be way inefficient.
Kevin Phillips
@phillipskevin
Jun 09 2016 18:37
anything you do is going to have to iterate through all the records
Sunil George
@georgesunil81
Jun 09 2016 18:37
Oh, derived property? Will it cause exclusion of records
Yes, agreed, but I want to handle it at the model level
Kevin Phillips
@phillipskevin
Jun 09 2016 18:38
you can do that too
Sunil George
@georgesunil81
Jun 09 2016 18:38
Just like the define - getter, setter that I use to modify some values. But, in this case I want to exclude some records altogether based on some condition.
Kevin Phillips
@phillipskevin
Jun 09 2016 18:39
where is the condition
Sunil George
@georgesunil81
Jun 09 2016 18:39
Right now it is in my controller (after calling the model and getting back the result set from the model)
Kevin Phillips
@phillipskevin
Jun 09 2016 18:40
can you store it to your model?
or is it specific to the controller?
Sunil George
@georgesunil81
Jun 09 2016 18:41
How do I do that? Any example that I can look at which does this in the model itself. I want to do it at the model level not in the controller.
dylanrtt
@dylanrtt
Jun 09 2016 18:41
@georgesunil81
activeTodos: {
    get() {
        return this.attr('todos').filter(todo => todo.attr('active'));
    }
}
you can add that to your model for instance
Sunil George
@georgesunil81
Jun 09 2016 18:42
Yep, something like that.
Kevin Phillips
@phillipskevin
Jun 09 2016 18:42
can.Model.extend({
    findAll: ‘/todos'
}, {
    define: {
        // this is where activeTodos would go
    }
});
Sunil George
@georgesunil81
Jun 09 2016 18:43
This is good. Thanks @dylanrtt @phillipskevin
My filtering condition is a little elaborate though. It is based on couple of dates and some other attribute values. So, I guess I could write some function that does this and choose to filter or not based on the truthy value the function returns.
I hope it is possible.
dylanrtt
@dylanrtt
Jun 09 2016 18:46
If the model findAll returns the list of todos for example, it may be more appropriate to add a prototype method to Todo.List that will return the filtered list
Sunil George
@georgesunil81
Jun 09 2016 19:27
Another quick question, is there way to do AND or OR in stash? I know stash is supposed to be logicless, but I do see some if and eq helpers, so was wondering if we could do AND/OR too?
*stache
Sunil George
@georgesunil81
Jun 09 2016 19:43
Thanks @dylanrtt for your input above.
dylanrtt
@dylanrtt
Jun 09 2016 20:54
@georgesunil81 There has been talking about adding them but for now I think you'll have to define them yourself. Rough example below:
can.stache.registerSimpleHelper('or', function(...args, options) {
    var show = false;

    can.each(args, function(arg) {
        if (arg) {
            show = true;
            return false;
        }
    });

    return show ? options.fn() : options.inverse();
});
Sunil George
@georgesunil81
Jun 09 2016 21:07
Thanks @dylanrtt , thats smart!