Prepopulate List from model

82 views
Skip to first unread message

Sergei Koba

unread,
May 1, 2014, 7:51:25 AM5/1/14
to backbon...@googlegroups.com
Hello,

I have a problem =) I wish to add existing items to List when my model is fetched.
I have such a model:

app.Role = Backbone.Model.extend({
urlRoot: '/roles/rest',
default:{
permissionList:[165]
},
schema: {
       definition:  { },
       permissionList: {type: 'List', itemType:'Select', options: new app.PermissionsCollection() }
   },
   parse: function(response) {
   return response.data;
}
});

PermissionsCollection is fetched from server

Now I wish to render a form and prepopulate my List with permissions, which role already has. How to do it?
This approach is not working:

var form = new app.EditForm({
   model: model,
   template: _.template($('#'+keyword+'-form-template').html())
}).render();

app.CurrentView = form;

$('#inner-content').html(form.el);


model.on('change', function(model) {
_.each(model.attributes, function(el,key,list){
var data = {};
data[key] = el;
app.CurrentView.setValue(data);
});
});

Sergei Koba

unread,
May 1, 2014, 11:56:54 AM5/1/14
to backbon...@googlegroups.com
I managed to do this by this:
model.on('change', function(model) {
_.each(model.attributes, function(el,key,list){
if (_.isArray(el)){
_.each(app.CurrentView.fields[key].editor.items, function(element, index, list){
    element.remove();
    });
_.each(el, function(element, index, list){
    app.CurrentView.fields[key].editor.addItem(element);
    });
} else {
var data = {};
data[key] = el;
app.CurrentView.setValue(data);
}
});
});


is it ok ? :D
Reply all
Reply to author
Forward
0 new messages