Skip to content

Commit 0902aaf

Browse files
authored
Merge pull request #5 from RoundingWellOS/update-build
Update build tools and bump version to 1.1.1
2 parents 0b79e06 + 813be6a commit 0902aaf

22 files changed

Lines changed: 5085 additions & 2230 deletions

.babelrc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"presets": [ "es2015" ]
3-
}
2+
"presets": ["@babel/env"],
3+
"env": {
4+
"test": {
5+
"plugins": ["istanbul"]
6+
}
7+
}
8+
}

.eslintrc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
2-
"parser": "babel-eslint",
32
"extends": "eslint:recommended",
3+
"parserOptions": {
4+
"ecmaVersion": 6,
5+
"sourceType": "module"
6+
},
47
"env": {
58
"node": true,
69
"mocha": true
710
}
8-
}
11+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
coverage
33
.DS_Store
4+
.nyc_output

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: node_js
22
node_js:
3-
- "6.0"
3+
- "12.0"

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2017 RoundingWell
3+
Copyright (c) 2020 RoundingWell
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,4 @@ Backbone.Store is heavily inspired by [Backbone.UniqueModel](https://github.com/
176176

177177
===
178178

179-
This library is © 2017 RoundingWell. Distributed under MIT license.
179+
This library is © 2020 RoundingWell. Distributed under MIT license.

dist/backbone.store.esm.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import _ from 'underscore';
2+
import Backbone from 'backbone';
3+
4+
/*
5+
* Encapsulates a cache for a single model.
6+
*/
7+
8+
function ModelCache(Model, modelName) {
9+
this.instances = {};
10+
this.Model = Model;
11+
this.modelName = modelName;
12+
this.ModelConstructor = this._getConstructor(Model);
13+
}
14+
15+
_.extend(ModelCache.prototype, {
16+
_getConstructor: function _getConstructor(Model) {
17+
var cache = this;
18+
19+
var ModelConstructor = function ModelConstructor(attrs, options) {
20+
return cache.get(attrs, options);
21+
}; // Extend Model's static properties onto new
22+
23+
24+
_.extend(ModelConstructor, Model); // Backbone collections need prototype of wrapped class
25+
26+
27+
ModelConstructor.prototype = this.Model.prototype;
28+
return ModelConstructor;
29+
},
30+
get: function get(attrs, options) {
31+
var instanceId = attrs && attrs[this.Model.prototype.idAttribute]; // Attempt to restore a locally cached instance
32+
33+
var instance = this.instances[instanceId];
34+
35+
if (!instance) {
36+
// If we haven't seen this instance before, start caching it
37+
return this._new(attrs, options);
38+
} // Otherwise update the attributes of the cached instance
39+
40+
41+
instance.set(attrs);
42+
Store.trigger('update', instance, this);
43+
return instance;
44+
},
45+
_new: function _new(attrs, options) {
46+
var instance = new this.Model(attrs, options);
47+
48+
if (instance.isNew()) {
49+
// Store the instance if we get an id after instantation
50+
instance.once("change:".concat(instance.idAttribute), this._add, this);
51+
} else {
52+
this._add(instance);
53+
}
54+
55+
instance.on('destroy', this.remove, this);
56+
return instance;
57+
},
58+
_add: function _add(instance) {
59+
// If the id is already stored do not add it.
60+
if (this.instances[instance.id]) return;
61+
this.instances[instance.id] = instance;
62+
Store.trigger('add', instance, this);
63+
},
64+
remove: function remove(instance) {
65+
if (!this.instances[instance.id]) return instance;
66+
delete this.instances[instance.id];
67+
Store.trigger('remove', instance, this);
68+
return instance;
69+
}
70+
});
71+
72+
var ModelCaches = {};
73+
/**
74+
* Store wrapper converts regular Backbone models into unique ones.
75+
*
76+
* Example:
77+
* const StoredUser = Store(User);
78+
*/
79+
80+
function Store(Model) {
81+
var modelName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _.uniqueId('Store_');
82+
var cache = Store.add(Model, modelName);
83+
return cache.ModelConstructor;
84+
} // Static functions
85+
86+
87+
_.extend(Store, Backbone.Events, {
88+
ModelCache: ModelCache,
89+
add: function add(Model, modelName) {
90+
if (!modelName) throw 'Model name required';
91+
if (ModelCaches[modelName]) return ModelCaches[modelName];
92+
return ModelCaches[modelName] = new Store.ModelCache(Model, modelName);
93+
},
94+
getCache: function getCache(modelName) {
95+
if (!ModelCaches[modelName]) throw "Unrecognized Model: \"".concat(modelName, "\"");
96+
return ModelCaches[modelName];
97+
},
98+
getAllCache: function getAllCache() {
99+
return _.clone(ModelCaches);
100+
},
101+
get: function get(modelName) {
102+
return Store.getCache(modelName).ModelConstructor;
103+
},
104+
getAll: function getAll() {
105+
return _.reduce(ModelCaches, function (all, cache, modelName) {
106+
all[modelName] = cache.ModelConstructor;
107+
return all;
108+
}, {});
109+
},
110+
remove: function remove(modelName) {
111+
delete ModelCaches[modelName];
112+
},
113+
removeAll: function removeAll() {
114+
ModelCaches = {};
115+
}
116+
});
117+
118+
Backbone.Store = Store;
119+
120+
export default Store;
121+
export { ModelCache };

0 commit comments

Comments
 (0)