Skip to content

Instantly share code, notes, and snippets.

View benpriebe's full-sized avatar

Ben benpriebe

  • Brisbane, Australia
View GitHub Profile
@benpriebe
benpriebe / object-creation.md
Last active July 13, 2023 21:29
Douglas Crockford - Create Object Recipe (2014)

Douglas Crockford showed a slide showing how he creates JavaScript objects in 2014.

He no longer uses Object.create(), avoids 'this' and doesn't even care about memory reduction by using prototypes.

https://www.youtube.com/watch?v=bo36MrBfTk4 (skip ahead to 35 mins for relevant section)

Here is the pattern described on the slide:

function constructor(spec) {

This gist highlights a more complex example on how to implement autofac delegating factories to create container driven dependencies with runtime paramaters.

Please see this article for a primer before delving into the code here:

http://www.codebullets.com/my-favorite-dependency-injection-container-1141

###The Example:###

Suppose you have a DocumentService whose responsiblity is to retrieve documents from a document repository.

@benpriebe
benpriebe / js-inheritance.md
Created December 6, 2012 01:39
JS Inheritance styles

#Inheritance Styles

Here are a few different ways to try and implement prototypal inheritance in a classical way. In these examples, we try to derive a more specific class of the base class - Asset.

By inspecting the web development tools console output in Chrome you can see the prototype chain and view the differences of each approach.

<html>
	<body>
@benpriebe
benpriebe / readme.md
Created November 12, 2012 03:37
Making web.config appSettings available via JavaScript (ASP.NET MVC)

Making web.config appSettings available via JavaScript (ASP.NET MVC)

You may wish to expose server side configuration via JavaScript. For example, you may have a different base url for your REST api per environment (dev,test,uat,prod) which your JavaScript AJAX calls need to consider.

Typically, you would push the configuration data down via an html5 data- attribute, a hidden input field or via explicit assignment to a JavaScript global. You usually end up doing this for every item you wish to expose. However, using a custom MVC ActionFilterAttribute and a Server-to-JSON serialization technique you can easily expose your web.config appSettings for controller actions.

NOTE: Exposing the entire appSettings configuration section may expose your application to security risks.

How

@benpriebe
benpriebe / readme.md
Created November 12, 2012 01:30
Transferring Server data to JavaScript with initial Page Request

Transferring Server data to JavaScript with initial Page Request

Often you want to return data with your call to retrieve a web page/control from ASP.NET MVC so that it can be accessed via JavaScript as a ViewModel (using KnockoutJS or equivalent).

Typically, you make a server call to retrieve the web page using the standard ASP.NET controller action and then make a AJAX call to go retrieve the model to populate your JavaScript ViewModel.

Wouldn't it be nice if you could send back the model with the page request and avoid a separate AJAX call?

How