angular-restmod
angular-restmod copied to clipboard
how to generate front-end specified attributes
Hi,I'm trying to convert raw data to front-end specified attributes
for example
raw data
{
"name": "jack",
"id": "1"
"roles": [{id:1,name:“manager”},{id:2,name: “seller”}]
}
//after user = User.$find(1)
{
"name": "jack",
"id": "1",
"roles": [{id:1,name:“manager”},{id:2,name: “seller”}],
"roleName": "manager,seller“
}
or
//raw data
{
"name": "jack",
"id": "1",
"role": {id:1,name:“manager”}
}
record data
{
"name": "jack",
"id": "1"
"role": {id:1,name:“manager”}
"roleClass": "red'
}
but customed attribute will never send to server
how define the model ?
For the first case I would do something like this.
restmod.model('user', function(){
'Record.roleName': { ignore: true },
$hooks: {
'after-feed': function(){
this.roleClass: _.pluck(this.roles, 'name').join(', ');
}
}
})
I haven't tested this, bu you can use the ignore property, that will prevent the property to be send to the server on any save operation (POST, PUT)
Also you can use the after-feed
hook to set the value of the this property every time the model change. here you can find more info about hooks.
I hope this helps..
Now you can also use computed properties..... docs are pending, but here is the PR where the functionality was added https://github.com/platanus/angular-restmod/pull/206/files
computed attribute not compatible with DirtyModel $restore function throw a exception: Cannot set property isOverstaffed of #<Model> which has only a getter