vendredi 19 juin 2015

Refreshing model when property changes

I have a conversation model filled up by fetched json data from my rails backend. I am using an ArrayController for the whole model. Each conversation then has its own itemController.

The problem I have is that I want to refresh my template if any of the item's property changes. Right now, it only refreshes if the status property changes and I don't know why (maybe it is because I am refreshing when queryParam status changes?)

For example, I have a function which assign the conversation to another subuser. So I am replacing the previous subuser with the one I am assigning the conv to.

Please if you need more infos tell me and I will gladly update if you think you can help me.

Here is the code for this function :

assignSingleConversation: function(subuser){

        var self = this;

        this.store.find('conversation', this.get('selectedConv').id).then(function(conversation){
            conversation.set('subuser', subuser);
            conversation.set('url', subuser.get('email')); <-- This doesnt update template
            conversation.set('status', 'opened'); <-- This updates templates

            var newAssignation = {
                changed_to : subuser.id,
            };

            //Manually doing ajax call using jquery
            $.ajax({
                type : 'PUT',
                url: '/conversations/' + conversation.id + '/assignation',
                dataType: 'json',
                data: newAssignation,
                success: function(data) {
                    if(conversation.get('content')){
                        conversation.get('content').pushObject(data);
                    }
                }
            });
        }).then(function(){
            self.send('refreshAndClear', self);
        });             
    },

For information, here is my model hook :

model: function(params){
    return this.store.filter('conversation', { status : params.status }, function(rec){
        if(params.status == 'all'){
            return ((rec.get('status') === 'opened' || rec.get('status') === 'closed'));
        }
        else{
            return (rec.get('status') === params.status);
        }
    });
},

And here is my model :

App.Conversation = DS.Model.extend({
    readStatus: DS.attr('string'),
    url: DS.attr('string'),
    status: DS.attr('string'),
    lastMessage: DS.attr('string'),
    createdAt: DS.attr('string'),
    timeAgoElement: DS.attr('string'),
    customer: DS.belongsTo('customer'),
    subuser: DS.belongsTo('subuser'),
    content: DS.attr('array'),
    //Model's own properties
    isChecked: false,
    isSelected: false,
    selectedClass: 'conversation-history'
}); 




Aucun commentaire:

Enregistrer un commentaire