bem-promised-models
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"description": "BEM registry for promised-models", | ||
"description": "BEM wrapper for promised-models", | ||
"name": "bem-promised-models", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"repository": "git@github.com:delfrrr/bem-promised-models.git", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
190
README.md
# bem-promised-models | ||
BEM wrapper for [promised-models](https://github.com/delfrrr/promised-models) | ||
## Key features | ||
* support for [BEM](https://en.bem.info) and [bem-node](https://github.com/bem-node/bem-node) | ||
* promise based ([Vow Promises](https://github.com/dfilatov/vow)) | ||
* typed attributes | ||
* nested models and collections | ||
* async calculations and validation | ||
* can be used in `priv.js` with bem-node's `i-state` | ||
## Install | ||
$ npm install --save bem-promised-models | ||
Add `node_modules/bem-promised-models/blocks` to your levels lists | ||
Add deps for promised-models | ||
``` | ||
({ | ||
mustDeps: [ | ||
{block: 'promised-models'} | ||
] | ||
}) | ||
``` | ||
For noBEM setups use [promised-models](https://github.com/delfrrr/promised-models) | ||
## Usage | ||
``` | ||
BEM.Model.decl('fashion-model', { | ||
attributes: { | ||
name: { | ||
type: 'String' | ||
} | ||
} | ||
}); | ||
var model = BEM.blocks['fashion-model'].create({ | ||
name: 'Kate' | ||
}); | ||
model.get('name'); //Kate | ||
``` | ||
### Extend | ||
Add declaration for existent one: | ||
``` | ||
BEM.Model.decl('fashion-model', { | ||
attributes: { | ||
sename: { | ||
type: 'String' | ||
} | ||
}, | ||
getFullName: function () { | ||
return [this.get('name') + this.get('sename')].join(' '); | ||
} | ||
}); | ||
var model = BEM.blocks['fashion-model'].create({ | ||
name: 'Kate', | ||
sename: 'Moss' | ||
}); | ||
model.getFullName(); //Kate Moss | ||
``` | ||
Inherit: | ||
``` | ||
BEM.Model.decl('uppercased-model', 'fashion-model', { | ||
getFullName: function () { | ||
return this.__base().toUpperCase(); | ||
} | ||
}); | ||
``` | ||
### Nested models and collections | ||
``` | ||
BEM.Model.decl('podium', { | ||
attributes: { | ||
//nested | ||
currentModel: { | ||
type: 'Model', | ||
modelType: 'fashion-model' | ||
}, | ||
//collections | ||
avaibleModels: { | ||
type: 'ModelsList', | ||
modelType: 'fashion-model' | ||
} | ||
} | ||
}); | ||
``` | ||
### Pass models by client id (for broser render only) | ||
``` | ||
//bh template | ||
var model = BEM.blocks['fashion-model'].create(); | ||
ctx.content({ | ||
block: 'view', | ||
js: { | ||
modelId: model.cid | ||
} | ||
}); | ||
//BEM.DOM declaration | ||
var model = BEM.blocks['fashion-model'].getOne(this.params.modelId); | ||
``` | ||
### Find models by storage id | ||
``` | ||
var model = BEM.blocks['fashion-model'].getAny(this.params.mongoId); | ||
//load data from storage | ||
model.fetch().done(); | ||
``` | ||
### Define storage | ||
``` | ||
BEM.Model.decl('fashion-model', { | ||
storage: { | ||
insert: function (model) { | ||
//... | ||
}, | ||
update: function (model) { | ||
//... | ||
}, | ||
find: function (model) { | ||
//... | ||
}, | ||
remove: function (model) { | ||
//... | ||
} | ||
} | ||
}); | ||
``` | ||
## API | ||
### BEM.Model | ||
#### `.decl(modelName, [baseModel], [properties], [staticProperties])` | ||
Adds model declaration | ||
#### `.getOne([cid])` | ||
Get model instance by client id `model.cid` | ||
### BEM.blocks['some-model'] | ||
#### `.create([id], [data])` | ||
Create model instance with storage id and data | ||
#### `.getOne([cid])` | ||
Get model instance of current class by client id `model.cid` | ||
#### `.getAny([id])` | ||
Get model instance by storage id `model.id`. If no instance with `id` was found, creates new one. | ||
### model instance | ||
See [promised-models](https://github.com/delfrrr/promised-models/blob/master/README.md#api-reference-in-progress) | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29829
192