How import client utility page in controller.js
-
I have this utility client page in lib folder.
'use strict'; define('utile', [], function() { var modulo = {}; modulo.getLimiteCaratterePost = function() { var limite_carattari_post = 25; return limite_carattari_post; }; return modulo; });
Now I want get this method in controller.js in the same folder of utile.js. My purpose is how can I import this function in my controller.js?Something linke this:
var utile=require(['utile']),
Anyone can help me?
-
@Doppy requirejs is async, and pass the module into a callback.
require(['x'], function (x) { .... });
-
@PitaJ I have in the same folder the controllers..js and utile.js so I import in this way:
require(['utile'], function (utile) { }
But I Obtain this error:
AssertionError: path must be a string at Module.require (module.js:496:3) at /vagrant/nodebb/node_modules/nodebb-plugin-connect-bookmarked/lib/controllers.js:35:39
This is my require-config file:
require.config({ baseUrl: config.relative_path + "/src/modules", waitSeconds: 7, urlArgs: "v=" + config['cache-buster'], paths: { 'forum': '../client', 'admin': '../admin', 'vendor': '../../vendor', 'plugins': '../../plugins' } });
I don't understand what I'm wronging?
-
@Doppy oh, you're using it server side? Then it's just
var utile = require('./utile');
But you should really look up how CommonJS and require.js/AMD work, because of you do the above the
define
doesn't work server side. -
@PitaJ thanks for you suggest! can I have a questione for you? I crate a file utile.js that I use in my client files like utilities method( like a factory method for client file). Your code doesn't work in my client files scripts. I understand that I can't use utile.js in the server side but does exist some pattern o some method that I can use to factory both as server side and client side?
-
@Doppy check out UMD: https://github.com/umdjs/umd