require

requirejs 关于shim的使用方式

纵然是瞬间 提交于 2019-12-05 01:38:21
一直在搞移动开发,很久没写过有关javaScript的文章了,最近公司开发了自己的web框架,用到了requirejs,之前用过,借此复习一下,特别是依赖问题。 requirejs具有以下是异步加载,当然已经加载的不会再次加载,这是非常好的一种优化。当然,我们这里来看看shim的作用。 导出非AMD模块化函数(模块化) hello.js function sayHello(name) { alert('Hi '+name); } requirejs.config({ baseUrl: '/public/js', paths: { hello: 'hello' //相对于baseUrl的方式导入hello.js ,并且给一个别名 }, shim: { hello: { exports: 'sayHello' } //因为是一个函数,使用export即可 } }); requirejs(['hello'], function(hello) { //function参数接收导出的函数名,实际是sayHello函数 hello(); }); 导出一个函数,意味着什么?意味着我们得到了一个javaScript类,所以已经满足了绝大多数需求。 但是有一个问题,在app.js中写了很多function,整合成一个function有点费劲,想直接导出如何做? 办法如下: hello.js

Lodash是什么?

梦想与她 提交于 2019-12-05 00:58:46
lodash:是一个一致性、模块化、高性能的 JavaScript 实用工具库。(也就是相当于自己封装的私有方法) node里引入 // Load the full build. var _ = require('lodash'); // Load the core build. var _ = require('lodash/core'); // Load the FP build for immutable auto-curried iteratee-first data-last methods. var fp = require('lodash/fp'); // Load method categories. var array = require('lodash/array'); var object = require('lodash/fp/object'); // Cherry-pick methods for smaller browserify/rollup/webpack bundles. var at = require('lodash/at'); var curryN = require('lodash/fp/curryN'); 举些使用的例子: _.chunk(array, [size=1]):按照size的大小来拆分array数组 _.chunk(['a

Catch an error on requiring module in node.js

為{幸葍}努か 提交于 2019-12-05 00:19:51
Can't seem to find any articles on this anywhere. I basically want to catch the "Cannot find module" error from within the program, and optionally ask to install it, but I can't seem to catch any errors even with a try/catch around my require statements. Is this even possible? I haven't seen it done anywhere. For example: try { var express = require('express'); } catch (err){ console.log("Express is not installed."); //proceed to ask if they would like to install, or quit. //command to run npm install } I suppose this could be done with a seperate .js startup file without any 3rd party

配置Apache使局域网内的设备都可以访问

二次信任 提交于 2019-12-04 23:53:25
1这里用wamp做演示,wamp2.5内置了Apache2.4.9 2我们打开Apache目录\wamp\bin\apache\apache2.4.9下的“conf”文件夹,完整路径:C:\wamp\bin\apache\apache2.4.9\conf,找到httpd.conf,用sublime text或者notepad等代码编辑器打开。 3找到 # onlineoffline tag - don't remove 下方的: Require local 4将 Require local 替换成: Require all granted 即可。 来源: oschina 链接: https://my.oschina.net/u/2541651/blog/626747

Browserify require returns an empty object

我怕爱的太早我们不能终老 提交于 2019-12-04 22:24:03
I have this code which, for reasons I can't understand, produces an empty object when using require() . My file structure is like this: src |__ public |__ javascript |__ collections | categories.js | listings.js <-- Always an empty object |__ models | category.js | employer.js | listing.js | location.js | routing | templates | tests | ui-components | views The problem file is collections/listings.js , which seems to simply output as an empty object when required like so: var ListingsCollection = require('../collections/listings') src/public/javascript/collections/listings.js looks like this:

Rails lib directory

狂风中的少年 提交于 2019-12-04 22:18:08
Question about lib directory. What are good practices in using the lib directory? When should it be used over app/models or app/helpers? And somewhat related how do you get Rails 3 to include files from the lib directory? Thanks One use of the lib directory (how I use it most often) is to share code between models to stay DRY. For example, if you are defining a tag_tokens attribute on many different models for use with a tokenizer input, you could put that in "tag_accessor.rb" or something, place it in /lib ', and then include it with include TagAccessor . The ruby file might look like: module

How to wait till require finished in dojo

試著忘記壹切 提交于 2019-12-04 20:16:48
I'm providing an infrastructure for other developers, and i'm using Dojo for it. In my Init function i'm using the 'require' method (of course) which one of the parameters are the callback function when all the modules have been loaded. The problem is that the client don't want to use callbacks. He wants to call me and line after to use me (to make my Init method synchronized) - and give him the code back after we for sure finished loading our modules. My Code <script src=..../dojo.js></script> function Init() { var loaded = false; require(["...myFiles..."], function() { loaded = true; }); //

php require class call from inside method

瘦欲@ 提交于 2019-12-04 18:55:12
问题 from my understanding, require pastes code into the calling php file. what if you were requiring from inside a method...it would paste the entire code/class inside the method, blocking the next statement in the method. eg. function test() { require 'pathtosomeclasscode'; somestatement; // any code after the require is blocked. } how do i get around this, to be able to require code where-ever, without it being pasted in that exact spot? 回答1: Somewhat surprisingly, this appears to work just

开始nodejs+express的学习+实践(8)

纵饮孤独 提交于 2019-12-04 16:49:40
1.session使用 介绍的非常详细: http://www.cnblogs.com/chenchenluo/p/4197181.html 对比我们的app.js需要引入express-session模块和使用这个模块,在package依赖,并加载。 我们修改app.js 我们既然有了session,我们开始测试使用,在访问首页我们就是设置session内容,当我们跳转时,比如到hello,我们是还能访问到设置session的值,这就表示session安装成功。 index.js对首页和hello修改,加入session赋值 app.get('/',function(req, res){ var currentpage=req.query.currentpage?req.query.currentpage:1; model.model_index(currentpage,function(items,len,allpage){ req.session.love = '我爱你'; res.render('index', { title: 'Express',hello: 'hello world!',arr: items,len:len,allpage:allpage,cur:currentpage,love: req.session.love }); }); }); app

RAILS_ROOT require?

我们两清 提交于 2019-12-04 16:49:08
问题 I'm trying to access the RAILS_ROOT constant in a file residing in the /lib directory, but I'm not able to (uninitialized constant error). Is there something that I need to require to be able to do this? 回答1: Yes, you should require the environment.rb: require File.dirname(__FILE__) + '/../config/environment.rb' puts RAILS_ROOT And Rails.root instead. 来源: https://stackoverflow.com/questions/2933225/rails-root-require