How can I pass values to a class constructor with inversifyJS? -


hi have typescript test project inversify , requirejs can cloned from: https://github.com/ahedreville/ioctest.git

his package.json follows

{    "name": "ioctest",    "version": "1.0.0",    "description": "test of ioc inversify , requirejs",    "scripts": {      "start": "http-server"    },    "author": "alexandre hedreville",    "license": "isc",    "devdependencies": {      "http-server": "^0.10.0"    },    "dependencies": {      "@types/requirejs": "^2.1.30",      "inversify": "^4.3.0",      "reflect-metadata": "^0.1.10",      "requirejs": "^2.3.4"    }  }

you can project running on browser issuing following commands...

mkdir temp  cd temp    git clone https://github.com/ahedreville/ioctest.git  cd ioctest    c:\temp\ioctest>tree /f /a    ...  c:.  |   app.main.js  |   index.html  |   package.json  |   readme.md  |   tsconfig.json  |  \---src          app.js          app.js.map          app.ts          ioc_config.js          ioc_config.js.map          ioc_config.ts          question.js          question.js.map          question.ts          question2.js          question2.js.map          question2.ts          types.js          types.js.map          types.ts  		  npm install    npm start    http://localhost:8080

as can see, opening browser developer console log, interface iproblem resolved question2 class requested on class ioc_config.ts

question2 class follows

import { injectable } "inversify";  import { iproblem } './types';    @injectable()  export class question2 implements iproblem {      public readonly factor: number = 0;      public count: number = 0;      public answer: number = 0;  }

but if change target resolution class on ioc_config.ts

changing ioc_config.ts class follows

import { container } 'inversify';  import { stypes, iproblem } './types';  import { question } './question';  import { question2 } './question2';    export const container = new container();    //container.bind<iproblem>(stypes.iproblem).to(question2);  container.bind<iproblem>(stypes.iproblem).to(question);

now resolution of iproblem interface fail since question.ts has constructor expect values passed into.

question class follows

import { injectable } "inversify";  import { iproblem } './types';    @injectable()  export class question implements iproblem {      public answer: number = 0;      constructor(public readonly factor: number, public readonly count: number) {      }  }

how can solve issue (i newbie on inversify) , pass values @ resolution time constructor?

thanks in advance


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -