yonatanmn/Cartiv

[patch request] API object is empty in components

ro0p opened this issue · 2 comments

ro0p commented

You example usage simply doesnt work for me, because "import API from './Api';" in components forces to create a new empty API object, so all storage definitions are lost.
Maybe I missed something, or trying to use your lib in a wrong way, but I cannot find out how the API object can be used as a global interface.
I can solved it with using singleton pattern for APIsHolder object. Here is the patch:

--- ./createAPIsHolder.orig.js 2016-09-29 17:17:55.000000000 +0200
+++ ./createAPIsHolder.js 2016-12-04 03:24:08.712551108 +0100
@@ -8,6 +8,7 @@
var _utils = require('./utils');

function createAPIsHolder() {
+ var _instance;
function APIsHolder() {}

function addAPIActions(apiName, actions) {
@@ -23,5 +24,7 @@
``
APIsHolder.prototype.addAPIActions = addAPIActions;

- return new APIsHolder();
-}
+ if (!_instance)
+ _instance = new APIsHolder();
+ return _instance;
+}

Thank you for this great lib!

@ro0p - this works as expected and no need of custom API holder needed.
You must be doing something different for which the API object is empty.
Could you post code and configuration or a gist with which the issue could be identified?

Thanks!

hey @ro0p , sorry for late response.

API is created once in a separate file. If your app is huge, you can create more than 1 api, but usually it's not needed.
Later, every store or component can require the api you've previously created.
the docs have full example.