theironcook / Backbone.ModelBinder

Simple, flexible and powerful Model-View binding for Backbone.
1.42k stars 159 forks source link

Nested Model #231

Open gabrielp16 opened 7 years ago

gabrielp16 commented 7 years ago

Hi,

I have a problem with a nested model: { name:'Gabo', scheduler:{ date: 1987198712, type: 'daily' } } the modelBind doesn't recognize the properties in scheduler, how can I do for make a bind in date and type?

Thank you for your attention

amakhrov commented 7 years ago

@gabrielp16 I assume you're talking about the scheduler attribute. Is it a real nested model (an instance of BB.Model with attributes date and type) or just a plain object (POJO)?

gabrielp16 commented 7 years ago

Is a a plain object (POJO)... that change the point of view?

amakhrov commented 7 years ago

In this case you can use converters, like that:

binder.bind(model, el, {
  scheduler: [
    {
      selector: '.date',
      converter: (dir, value) => {
        if (dir === ModelBinder.Constants.ModelToView) return value.date;
        else return Object.assign({}, model.get('scheduler'), {date: value});
      }
    },
    // ... and a similar binding for "type"
  ],
});