1.1.8 • Published 5 years ago

bem-promised-models v1.1.8

Weekly downloads
3
License
-
Repository
github
Last release
5 years ago

bem-promised-models Build Status

BEM wrapper for promised-models2

Key features

  • support for BEM and bem-node
  • promise based (Vow Promises)
  • 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', elems: ['model', 'registry', 'registry-storage']}
    ]
})

For noBEM setups use promised-models2

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 browser 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])

Add 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, create new one.

model instance

See promised-models2

1.1.8

5 years ago

1.1.7

6 years ago

1.1.6

7 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago