javascript - reuqure.js how to separate main module to submodules -
guys. have question how can use require.js in project.
i have module. example:
obj.js
var mymodule = (function(){ var myobject = function(){ this.options = { foo: 'foo', bar: 'bar' }; }; myobject.prototype.foo = function(){ console.log(this.options.foo); }; myobject.prototype.bar = function(){ console.log(this.options.bar); }; return { getmyobject : function(){ return new myobject(); } } })();
i haven't problems using object solid. appended obj.js code:
define(function(){ return mymodule; })
and use in app.js module
requirejs.config({ baseurl : 'lib', paths : { app:'../js/app' } }); require(['app/obj'], function(mymodule){ var someobj = mymodule.getmyobject(); someobj.foo(); someobj.bar(); });
it's ok. how can split mymodule submodules? example. in module have 1 object, constructor , 2 methods. can put constructor file, example 'mymodule.myobject.js', foo 'mymodule.myobject.foo.js' , bar 'mymodule.myobject.bar.js' , use require.js? if it's really, how can this?
you can use way. expect following file structure
- js -- constructors --- objectcostructor.js -- objects --- objectmodule.js
so objectcostructor.js
is:
define(function(){ return myobject = function(){ this.options = { foo: 'foo', bar: 'bar' } } })
and objectmodule.js
define(['constructors/objectcostructor'], function(const){ const.prototype.foo = function(){ console.log(this.options.foo); }; const.prototype.bar = function(){ console.log(this.options.bar); }; return { getmyobject : function(){ return new const(); } } })
Comments
Post a Comment