mercredi 29 avril 2015

Handle Softdeletable Entities in Ember Store

We had to add a deleted flag to the entities we used in our application.

For example, our Answer entity looks like this:

App.Answer = DS.Model.extend({
    text: attr('string'),
    score: attr('number'),
    question: belongsTo('question', {inverse: 'answers'}),
    deleted: attr('boolean', {defaultValue: false})
});

A Question entity looks like this:

App.Question = DS.Model.extend({
    text: attr('string'),
    nextQuestions: hasMany('question'),
    answers: hasMany('answer', {inverse: 'question'}),
    deleted: attr('boolean', {defaultValue: false})
});

The same changes apply to all our 15+ entities. Note: we load all the data from server at once and use ember localstorage adapter.

The problem: The application can be 'virtually' splitted into 2 module: admin section (where I must display the deleted entities too) and the rest of the application, used by normal users who must not see the deleted entities.

One solution: So, in some screens I could use the getter directly, like: questions.get('answers'), in others I would have to filter the answers and skip the ones which are deleted. The easiest way would be to use an Ember.computed helper.

App.Question = DS.Model.extend({
        text: attr('string'),
        nextQuestions: hasMany('question'),
        answers: hasMany('answer', {inverse: 'question'}),

        activeAnswers: Ember.computed.filterBy('answers', 'deleted', false)

        deleted: attr('boolean', {defaultValue: false})
    });

Still, this means that we have to re-factor all our entities and all our controllers, which is a major change and will take a lot of effort.

Others solutions I'm thinking on emulating 2 ember stores if that's possible and use them separately, one for the admin screens which would contain the deleted entities and the other one for the rest of the application.

Does anyone see a better approach for this change? Something which could work at ember's store level and would not affect the models and controllers?

Thanks! Any advice is greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire