-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.hbs
129 lines (99 loc) · 2.56 KB
/
readme.hbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Intro
Give your data a place to rest its head. Easy data storage and retrieval, with the ability to use factories.
# Install
**Using bower:**
```bash
$ bower install mochila --save
```
Use your favorite flavor in the `dist/` directory. Under AMD it registers the module name: `mochila`. As a global, use: `Mochila`.
**Using npm:**
```bash
$ npm install mochila --save
```
Use `var x = require('mochila')` in your code.
# Simple usage examples
```js
var mochila = new Mochila();
var monsters = [
{
id: 'abc-222-xu985',
name: 'Mothra'
},
{
id: 'xyz-333-pk254',
name: 'Godzilla',
stats: {
heightFt: 330,
weightTons: 60000,
fistSizeFt: 36,
shoeSizeFt: 74
}
}
];
var found;
mochila.addCollection('monster');
mochila.load('monster', monsters);
found = mochila.find('monster', 'abc-222-xu985');
found.name === 'Mothra' // true
found = mochila.find('monster', 'xyz-333-pk254');
found.name === 'Godzilla' // true
typeof found.stats // 'object'
found.stats === monsters[1].stats // false -- `load` does a deep clone if no factory
found.kingOfMonsters // undefined
// loading an object with the id of one already in the database merges the two
mochila.load('monster', {
id: 'xyz-333-pk254',
name: 'Gojira',
kingOfMonsters: true
});
found.name === 'Gojira' // true
found.kingOfMonsters // true
```
Using factories:
```js
function Widget(opts) {
this.id = opts.id;
this.name = opts.title;
this.dimensions = opts.dimensions;
this.countId = ++count;
}
// when a factory is registered, its `create` property is passed the object from `load`
Widget.create = function(opts) {
return new this(opts);
};
var mochila = new Mochila();
var count = 0;
var cube = {
id: 0,
title: 'Cube',
isCube: true,
dimensions: {
width: 10,
height: 10,
depth: 10,
},
};
var found;
mochila.addCollection('widget');
mochila.addFactory('widget', Widget);
mochila.load('widget', cube);
found = mochila.find('widget', 0);
found instanceof Widget // true
found.title // undefined
found.name // 'Cube'
found.isCube // undefined
found.countId // 1
typeof found.dimensions // 'object'
found.dimensions === cube.dimensions // true -- factory did a shallow copy
```
**Note:** Make sure your data records each have an `id` property. It's used to store and search records efficiently. `id` can be a number or a string.
# API Reference
{{#module name="mochila"}}
{{>body~}}
{{>member-index~}}
{{>separator~}}
{{>members~}}
{{/module}}
* * *
# License
MIT