-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 14,7 @@ NodeJS. | |
- [Defining Models](#defining-models) | ||
- [Abbreviated syntax](#abbreviated-syntax) | ||
- [Adapters](#adapters) | ||
- [Using REST-Adapter in Browser](#using-rest-adapter-in-browser) | ||
- [Datatypes](#datatypes) | ||
- [Creating instances](#creating-instances) | ||
- [Validations](#validations) | ||
|
@@ -376,6 377,44 @@ myAdapter.addListener('error', function (err) { | |
}); | ||
``` | ||
#### Using REST-Adapter in Browser | ||
You can use the REST-Adapter in the browser. | ||
Assuming you use a tool like browserify to include `model` in your JS: | ||
```javascript | ||
var model = require('model'); | ||
var RestAdapter = require('model/lib/adapters/rest').Adapter; | ||
|
||
model.defaultAdapter = new RestAdapter({ | ||
host: window.location.protocol '//' window.location.hostname ':' window.location.port '/', | ||
defaultDataType: 'json', | ||
defaultFormat: 'json', | ||
crossDomain: false, | ||
cacheLifetime: 5000, | ||
camelize: true | ||
}); | ||
``` | ||
You can then access the restApi and add filters as you need: | ||
```javascript | ||
var restApi = model.defaultAdapter.restApi; | ||
|
||
// convert params to json | ||
restApi.addFilter('param', rest.RestFilters.PARAM_FILTER_JSON); | ||
``` | ||
After that you will also need to register all your models manually: | ||
```javascript | ||
var model = require('model'); | ||
|
||
model.register('User', require('app/models/user').User); | ||
model.register('Event', require('app/models/event').Event); | ||
``` | ||
## Creating instances | ||
Creating an instance of one of these models is easy: | ||
|
@@ -1263,4 1302,3 @@ Configure adapter options by creating a `test/db.json` file. See | |
- - - | ||
Model JavaScript ORM copyright 2112 [email protected]. | ||