From 67b85ac7a397b14b0630ce2e72b5e0f6ec578e90 Mon Sep 17 00:00:00 2001 From: Pau Ramon Revilla Date: Fri, 13 Oct 2017 21:54:21 +0200 Subject: [PATCH] Update README.md Added documentation for: - Default attributes - Using `has` to complement `get` --- README.md | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ac01c88..2fbe210 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,23 @@ them with the server. Apart from its attributes, a `Model` also holds the state the interactions with the server so you can react to those easily (showing loading states for instance). +#### `constructor(attributes: Object)` + +Initialize the model with the given attributes. + +You can also overwrite it to provide default attributes like this: + +```js +class User extends Model { + constructor(attributes) { + super(Object.assign({ + token: null, + email_verified: false, + }, attributes)) + } +} +``` + #### `attributes: ObservableMap` An `ObservableMap` that holds the attributes of the model. @@ -85,10 +102,6 @@ A `Request` object that represents the state of the ongoing request, if any. An `Error` object that represents the state of the failed request, if any. -#### `constructor(attributes: Object)` - -Initialize the model with the given attributes. - #### `toJS(): Object` Return the object version of the attributes. @@ -139,7 +152,20 @@ user.get('id') // => 1 #### `get(attribute: string): any` -Get the given attribute. If the attribute does not exist, it will throw. +Get the given attribute. If the attribute does not exist, it will throw an error. + +If different resources have different schemas you can +always use `has` to check whether a given attribute exists or not. + +Example: + +```js +if (user.has('role')) { + return user.get('role') +} else { + return 'basic' +} +``` #### `has(attribute: string): boolean`