U
U
uSide2014-03-21 11:00:02
backbone.js
uSide, 2014-03-21 11:00:02

Backbone - how to change Model in Collection?

There is a simple model and collection:

MG.Models.User = Backbone.Model.extend({
  defaults: {
    firstname: '',
    nickname: '',
    lastname: '',
    _id:''
  },
    urlRoot:"/users",
  idAttribute: "_id"
});

MG.Collections.Users = new (Backbone.Collection.extend({
  model: MG.Models.User,
    url:"/users",
  idAttribute: "_id"
}))();

Next, we make a view and render it in the layout region. The timeout function reloads the model after 10 seconds. In the collection, it basically changes, i.e. updating the model from the collection works, but the view associated with it is not redrawn:
MG.Views.User = Backbone.Marionette.ItemView.extend({
  template: "#template-user"
});
MG.Layout.content.show(new MG.Views.User({
  model: MG.Collections.Users.findWhere({_id:"номер юзера"})
}));
setTimeout(function(){
        MG.Collections.Users.findWhere({_id:"номер юзера"}).fetch();
},10000);

I would be very grateful if you explain how to make the associated view redraw

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Markov, 2014-05-03
@manameiz

view.listenTo(@model, 'change', view.render)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question