Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor http, validation and change events to separate plugins #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
components
build
node_modules
.DS_Store
components
node_modules
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@

SRC = $(wildcard lib/*.js)

build: components $(SRC)
build: components lib/*
@component build --dev

components:
clean:
@rm -rf build components node_modules

components: component.json
@component install --dev

clean:
rm -fr build components template.js
node_modules: package.json
@npm install

test:
@node test/server
test: build
@component test phantom

.PHONY: clean test
.PHONY: clean test
236 changes: 50 additions & 186 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,224 +1,88 @@
# model

W.I.P minimalistic extensible model component.
A super simple, pluggable model.

## API
## Installation

### model(name)
$ component install component/model

Create a new model with the given `name`.
## Example

```js
var model = require('model');
var User = model('User');
```

### .attr(name, [meta])
var defaults = require('model-defaults');
var http = require('model-http');

Define an attribute `name` with optional `meta` data object.

```js
var model = require('model');
/**
* User model.
*/

var Post = model('Post')
var User = model()
.use(http('/users'))
.use(defaults())
.attr('id')
.attr('title')
.attr('body')
.attr('created_at')
.attr('updated_at')
```

With meta data used by plugins:

```js
var model = require('model');

var Post = model('Post')
.attr('id', { required: true, type: 'number' })
.attr('title', { required: true, type: 'string' })
.attr('body', { required: true, type: 'string' })
.attr('created_at', { type: 'date' })
.attr('updated_at', { type: 'date' })
```

### .validate(fn)

TODO: validation callback docs

### .use(fn)

TODO: plugin docs

### .url([path])

Return base url, or url to `path`.

```js
User.url()
// => "/users"

User.url('add')
// => "/users/add"
```

### .route(path)

Set base path for urls.
Note this is defaulted to `'/' + modelName.toLowerCase() + 's'`

```js
User.route('/api/u')

User.url()
// => "/api/u"

User.url('add')
// => "/api/u/add"
```

### .headers({header: value})

Sets custom headers for static and method requests on the model.

```js
User.headers({
'X-CSRF-Token': 'some token',
'X-API-Token': 'api token'
.attr('name')
.attr('email')
.attr('created', { default: function () { return new Date(); }})
.attr('version', { default: 1 });

/**
* In use...
*/

var user = new User({ name: 'Fred' });
user.name('George');
user.email('[email protected]');
user.save(function (err) {
if (err) throw err;
console.log('saved', user.toJSON());
});
```

### .ATTR()

"Getter" function generated when `Model.attr(name)` is called.

```js
var Post = model('Post')
.attr('title')
.attr('body')

var post = new Post({ title: 'Cats' });

post.title()
// => "Cats"
```

### .ATTR(value)

"Setter" function generated when `Model.attr(name)` is called.

```js
var Post = model('Post')
.attr('title')
.attr('body')

var post = new Post;

post.title('Ferrets')
post.title()
// => "Ferrets"
```

- Emits "change" event with `(name, value, previousValue)`.
- Emits "change ATTR" event with `(value, previousValue)`.

```js
post.on('change', function(name, val, prev){
console.log('changed %s from %s to %s', name, prev, val)
})

post.on('change title', function(val, prev){
console.log('changed title')
})

```

### .isNew()

Returns `true` if the model is unsaved.

### .toJSON()

Return a JSON representation of the model (its attributes).

### .has(attr)

Check if `attr` is non-`null`.

### .get(attr)

Get `attr`'s value.

### .set(attrs)

Set multiple `attrs`.

```js
user.set({ name: 'Tobi', age: 2 })
```

### .changed([attr])

Check if the model is "dirty" and return an object
of changed attributes. Optionally check a specific `attr`
and return a `Boolean`.

### .error(attr, msg)
## Plugins
- TODO

Define error `msg` for `attr`.
## API

### .isValid()
#### model()

Run validations and check if the model is valid.
Return a new model constructor.

```js
user.isValid()
// => false
#### .use(fn)

user.errors
// => [{ attr: ..., message: ... }]
```
Use the given plugin `fn`.

### .url([path])
#### .primary(key)

Return this model's base url or relative to `path`:
Set the model's primary `key`, which defaults to `'id'`.

```js
var user = new User({ id: 5 });
user.url('edit');
// => "/users/5/edit"
```
#### .attr(name, options)

### .save(fn)
Define a new model attr by `name` with optional `options`. Doing this will create a getter/setter method on the prototype with the same `name`.

Save or update and invoke the given callback `fn(err)`.
#### #primary([value])

```js
var user = new User({ name: 'Tobi' })
Get the value of the model's primary attribute, or set it to `value`.

user.save(function(err){
#### #<attr>([value])

})
```
Get the value of an `<attr>`, or set it to a `value`.

Emits "save" when complete.
#### #set(obj)

### .destroy([fn])
Set multiple attributes at a time with an `obj`.

Destroy and invoke optional `fn(err)`.
#### #get(attr)

Emits "destroy" when successfully deleted.
Get the value of `attr`.

## Links
#### #has(attr)

- [Plugins](https://github.com/component/model/wiki/Plugins) for model
Check if `attr` is not null or undefined.

## Testing
#### #toJSON()

```
$ npm install
$ make test &
$ open http://localhost:3000
```
Return a JSON representation of the model.

# License

Expand Down
30 changes: 13 additions & 17 deletions component.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
{
"name": "model",
"repo": "component/model",
"description": "Elegant data models",
"version": "0.1.1",
"version": "1.0.0",
"license": "MIT",
"description": "A super simple, pluggable model.",
"keywords": [
"collection",
"model",
"orm",
"db",
"database"
],
"dependencies": {
"component/each": "0.0.1",
"component/emitter": "1.0.1",
"component/collection": "*",
"visionmedia/superagent": "0.15.1"
},
"development": {
"component/reactive": "0.13.0",
"component/assert": "*",
"component/domify": "*"
},
"main": "lib/index.js",
"scripts": [
"lib/index.js",
"lib/static.js",
"lib/proto.js"
"lib/protos.js",
"lib/statics.js"
],
"main": "lib/index.js",
"license": "MIT"
"dependencies": {
"component/clone": "0.1.1",
"component/emitter": "1.1.1"
},
"development": {
"component/assert": "*"
}
}
Loading