Skip to content

how to generate front-end specified attributes #195

Description

@sandwich99

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 ?

Activity

  1. blackjid commented on Oct 28, 2014

    @blackjid
    Member

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

  2. blackjid commented on Jan 8, 2015

    @blackjid
    Member

    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

  3. sandwich99 commented on Feb 6, 2015

    @sandwich99
    Author

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

  4. added this to the 1.3 milestone on Feb 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions