|
| 1 | +--- |
| 2 | +title: Model Options |
| 3 | +description: 'Model Options.' |
| 4 | +position: 6 |
| 5 | +category: API |
| 6 | +--- |
| 7 | + |
| 8 | +## Global Options |
| 9 | + |
| 10 | +It's recommended to define the global options in your [Base Model](/configuration#creating-a-base-model), |
| 11 | +in order to abstract configuration from your models. |
| 12 | + |
| 13 | +### `$http` |
| 14 | +- Returns: `HTTP Client Instance` |
| 15 | + |
| 16 | +Instance of the HTTP client which is used to make requests. |
| 17 | + |
| 18 | +See [Installation](/installation) |
| 19 | + |
| 20 | +### `baseURL` |
| 21 | +- Returns: `string` |
| 22 | + |
| 23 | +Base URL which is used and prepended to make requests. |
| 24 | + |
| 25 | +See [Configuration](/configuration#creating-a-base-model) |
| 26 | + |
| 27 | +```js |
| 28 | +baseURL() { |
| 29 | + return 'http://my-api.com' |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### `request` |
| 34 | +- Arguments: `(config)` |
| 35 | +- Returns: `HTTP Client Request` |
| 36 | + |
| 37 | +Request method which is used to make requests. |
| 38 | + |
| 39 | +See [Configuration](/configuration#creating-a-base-model) |
| 40 | + |
| 41 | +```js |
| 42 | +request(config) { |
| 43 | + return this.$http.request(config) |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### `parameterNames` |
| 48 | +- Returns: `object` |
| 49 | + |
| 50 | +This method can be overridden in the model to customize the name of the query parameters. |
| 51 | + |
| 52 | +See [Configuration](/configuration#customizing-query-parameters) |
| 53 | + |
| 54 | +```js |
| 55 | +parameterNames() { |
| 56 | + return { |
| 57 | + include: 'include', |
| 58 | + filter: 'filter', |
| 59 | + sort: 'sort', |
| 60 | + fields: 'fields', |
| 61 | + append: 'append', |
| 62 | + page: 'page', |
| 63 | + limit: 'limit' |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +#### `include` |
| 69 | +- Default: `include` |
| 70 | +- Returns: `string` |
| 71 | + |
| 72 | +#### `filter` |
| 73 | +- Default: `filter` |
| 74 | +- Returns: `string` |
| 75 | + |
| 76 | +#### `sort` |
| 77 | +- Default: `sort` |
| 78 | +- Returns: `string` |
| 79 | + |
| 80 | +#### `fields` |
| 81 | +- Default: `fields` |
| 82 | +- Returns: `string` |
| 83 | + |
| 84 | +#### `append` |
| 85 | +- Default: `append` |
| 86 | +- Returns: `string` |
| 87 | + |
| 88 | +#### `page` |
| 89 | +- Default: `page` |
| 90 | +- Returns: `string` |
| 91 | + |
| 92 | +#### `limit` |
| 93 | +- Default: `limit` |
| 94 | +- Returns: `string` |
| 95 | + |
| 96 | +## Model Options |
| 97 | + |
| 98 | +These are model-related options. |
| 99 | + |
| 100 | +### `resource` |
| 101 | +- Returns: `string` |
| 102 | + |
| 103 | +Resource route of the model which is used to build the query. |
| 104 | + |
| 105 | +See [Configuration](/configuration#creating-the-domain-models) |
| 106 | + |
| 107 | +```js |
| 108 | +resource() { |
| 109 | + return 'resource' |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### `primaryKey` |
| 114 | +- Default: `id` |
| 115 | +- Returns: `string` |
| 116 | + |
| 117 | +Primary key of the model which is used to build the query. |
| 118 | + |
| 119 | +See [Configuration](/configuration#changing-the-primary-key) |
| 120 | + |
| 121 | +```js |
| 122 | +primaryKey() { |
| 123 | + return 'id' |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### `relations` |
| 128 | +- Returns: `object` |
| 129 | + |
| 130 | +This method can be implemented in the model to apply model instances to eager loaded relationships. |
| 131 | +It works for collections too. |
| 132 | + |
| 133 | +It must return an object, which the key is the property of the relationship, and the value is the |
| 134 | +model instance. |
| 135 | + |
| 136 | +See [Configuration](/configuration#eager-loaded-relationships) |
| 137 | + |
| 138 | +```js |
| 139 | +relations() { |
| 140 | + return { |
| 141 | + comments: Comment |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### `hasMany` |
| 147 | +- Arguments: `(model)` |
| 148 | +- Returns: `Model` |
| 149 | + |
| 150 | +This method can be used to lazy load relationships of a model and apply model instances to them. |
| 151 | + |
| 152 | +It must receive a model instance as argument. |
| 153 | + |
| 154 | +See [Configuration](/configuration#lazy-loading-relationships) |
| 155 | + |
| 156 | +```js |
| 157 | +comments() { |
| 158 | + return this.hasMany(Comment) |
| 159 | +} |
| 160 | +``` |
0 commit comments