justmoon / constitute Goto Github PK
View Code? Open in Web Editor NEWMinimalistic Dependency Injection (DI) for ES5/ES6/ES7
Minimalistic Dependency Injection (DI) for ES5/ES6/ES7
I apologize for my stupid question beforehand.
I was trying to mock constitute, using
jest.mock( 'constitute' )
but it seems to throw me an error
TypeError: _dec is not a function
Is there a way where I can mock constitute function, properly?
Hello,
I think it would be great to support libraries on the container that don't need to be instantiated and can be referenced by using the library itself. Currently I have to do this:
libraries.js:
import $ from 'jquery';
import { Value } from 'constitute';
export const jQuery = new Value($);
Then, I have to find wherever the libraries.js
file is located relative to my current script and pass in the correct value.
test.js:
import constitute, { Dependencies, Value } from 'constitute';
import {jQuery} from '../some/path/to/lib/libraries.js';
@Dependencies(jQuery)
class Test {
constructor($) {
this.$ = $;
}
}
While this isn't the worst thing in the world, I think it would be nice to be able to reference jQuery as a dependency and the system knows to treat it as a Value
(aka library in my case).
config.js:
import _ from 'lodash';
import $ from 'jquery';
import constitute from 'constitute';
// configuration
constitute.addValues([_, $, ...etc]);
example.js:
import _ from 'lodash';
import $ from 'jquery';
import constitute, { Dependencies } from 'constitute';
@Dependencies(_, jQuery)
class Test {
constructor(_, $) {
this._ = _;
this.$ = $;
}
}
I think this way we could continue to import libraries as we normally do while letting constitute replace it where necessary if we overwrite it. I'm not sure how this works under the hood to know if it's even possible but I think it would make things a little more maintainable. Or is there perhaps a better way mockup when I did in the first scenario?
Thanks!
I want to use something like constitute(Person, user_id) for new Person(depA, depB, user_id), is it possible?
Consider the following code example:
class Foo {
bar = 'should be overwritten';
}
let foo1 = constitute(Foo);
let foo2 = constitute(Foo);
foo1.bar = 'i have the new value!';
console.log('foo 1:');
console.log(foo1.bar);
console.log('foo 2:');
console.log(foo2.bar);
In this case, constitute should have cached the instance of Foo
when setting foo1
. Then foo2
should therefor use the same instance as foo1
;
However this is the console output I received:
foo 1:
> i have the new value!
foo 2:
> should be overwritten
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.