vendredi 20 février 2015

Ember.js: How to save model with many-to-many relationship?

I have two models (Role, Permission) with many-to-many relationship. There is a list of permissions and role can have some of them. I want to create edit page for Roles and add there Permissions as a checkboxes list.


edit template:



<form>
{{input type="text" placeholder="role name" value=name}}
<br />
{{#each permissions_list}}
<label>
{{input type="checkbox" name="permissions[]"}}{{name}}
</label><br />
{{/each}}
<button {{action 'save'}}>Edit</button>
</form>


models:



var Role = DS.Model.extend({
agents: DS.hasMany('agent', {async: false}),
name: DS.attr('string'),
permissions: DS.hasMany('permission', {async: true})
});

var Permission = DS.Model.extend({
type: DS.attr('string'),
name: DS.attr('string'),
roles: DS.hasMany('permission', {async: false})
});


edit role controller



export default Ember.ObjectController.extend({
permissions_list: [],
actions: {
save: function() {
return this.get('model').save();
}
},
load_permissions: function() {
this.set('permissions_list', this.store.find('permission'));
}.on('init')
});


The problems:



  1. I can't find out how to mark checkbox as checked if this permissions is already in model.permissions

  2. I can't save permissions on on role saving. Controller does not see "permissions" data (this.get('permissions') in edit role controller)


permissions - list of permissions, that role contains


permissions_list - list of all permissions at database


Aucun commentaire:

Enregistrer un commentaire