angular-restmod icon indicating copy to clipboard operation
angular-restmod copied to clipboard

how to generate front-end specified attributes

Open sandwich99 opened this issue 9 years ago • 3 comments

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 ?

sandwich99 avatar Oct 28 '14 06:10 sandwich99

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..

blackjid avatar Oct 28 '14 12:10 blackjid

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

blackjid avatar Jan 08 '15 12:01 blackjid

computed attribute not compatible with DirtyModel $restore function throw a exception: Cannot set property isOverstaffed of #<Model> which has only a getter

sandwich99 avatar Feb 06 '15 05:02 sandwich99