Skip to content

Instantly share code, notes, and snippets.

View rexcfnghk's full-sized avatar

Rex Ng rexcfnghk

View GitHub Profile
const http = require('http');
var innerI,
i = 0,
urls = [ 'http://www.google.com', 'http://www.yahoo.com' ],
urlLength = urls.length;
for (; i < urlLength; i++) {
innerI = i;
http.get(urls[i], function() { console.log('i:' + innerI); });
var http = require('http');
function makeResponseHandler(i) {
return function(response) {
console.log('i: ' + i);
};
}
var i = 0,
urls = [ 'http://www.google.com', 'http://www.yahoo.com' ],
const http = require('http');
var urls = [ 'http://www.google.com', 'http://www.yahoo.com' ],
urlLength = urls.length;
for (let i = 0; i < urlLength; i++) {
http.get(urls[i], function() { console.log('i: ' + i); });
}
const http = require('http'),
urls = [ 'http://www.google.com', 'http://www.yahoo.com' ];
urls.forEach((e, i) => http.get(e, _ => console.log('i: ' + i)));
public abstract class BaseController : Controller
{
protected BaseController(UserProfile userProfile)
{
UserProfile = userProfile;
}
protected UserProfile UserProfile { get; }
}
public class AppNinjectModule : NinjectModule
{
public override void Load()
{
// UserProfileManager is just a factory that manages the life cycle
// of UserProfile, because it has dependencies of its own (e.g. WCF
// service client)
Bind<UserProfile>()
.ToMethod(ctx => ctx.Get<UserProfileManager>().GetUserProfile())
.InRequestScope();
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
public class HomeControllerTests
{
public class HomeController : BaseController
{
public HomeController(UserProfile userProfile) : base(userProfile)
{
}
public ActionResult Index()
{
return View();
}
public class UserProfileActionFilterAttribute : ActionFilterAttribute
{
private readonly UserProfile _userProfile;
public UserProfileActionFilterAttribute(UserProfile userProfile)
{
_userProfile = userProfile;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
public class AdminController : Controller
{
public ActionResult Index()
{
// In real production code you should probably use the AuthorizeAttribute
// instead of authorizing/forbidding the user inside an action method
var user = (UserProfile)ViewBag.User;
if (!user.IsAdmin)
{
return new HttpStatusCodeResult(HttpStatusCode.Forbidden);