S
S
Sergey Pronin2015-05-19 22:21:18
backbone.js
Sergey Pronin, 2015-05-19 22:21:18

How to set the default parameter of the second level?

There is a model.

App.Models.Dialog = Backbone.Model.extend({
    defaults:{
        'author': {
            avatar: '/img/myAvatar.png'
        },
        'opponent': {
            avatar: '/img/myAvatar.png'
        }
    }
});

If the author comes from the server, then the default avatar is not taken into account. Can I somehow solve it beautifully and force it to take the default one?
author: {
    id: 1,
    name: "[email protected]"
},

just came to mind
initialize: function() {
        var tmp = _.clone(this.get('author'));
        if(!tmp.avatar){
            tmp.avatar = '/img/myAvatar.png';
            this.set('author', tmp);
        }
        tmp = _.clone(this.get('opponent'));
        if(!tmp.avatar){
            tmp.avatar = '/img/myAvatar.png';
            this.set('opponent', tmp);
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Ostanin, 2015-05-20
@bazilio91

https://github.com/powmedia/backbone-deep-model might help. There, when assigning, deep extend is used.

A
Alexander Prozorov, 2015-05-20
@Staltec

Use the model's parse() method to intercept and adapt data coming from the server.
Read more in the official documentation: Model-parse

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question