This is a simple demo of how to create and use a Response Interceptor in AngularJS. In this case we are using the interceptor to convert the objects created from a JSON data load into typed instances of a custom "class".
module.exports = function(grunt) { | |
grunt.initConfig({ | |
inline: { | |
'index.html': ['tpl/*.html'] | |
} | |
}); | |
grunt.registerMultiTask('inline', 'Inline AngularJS templates into single file.', function() { | |
var SCRIPT = '<script type="text/ng-template" id="<%= id %>"><%= content %></script>\n'; |
(Full description and list of commands at - https://npmjs.org/doc/index.html)
Make sure to export your local $PATH and prepand relative ./node_modules/.bin/:
I recently began working with Node and MongoDB for a small personal project, largely just to learn the technologies. One thing that is fairly simple but that I found far from obvious and lacking in concrete examples was how to populate the part of my database that used referenced collections from the sample JSON data I was starting with. This post attempts to fill that gap using the following code snippets, which are heavily commented inline. You will notice I am using the awesome Mongoose library which makes working with MongoDB very easy.
http.createServer( app ).listen( app.get( 'port' ), function() {
mongoose.connect( 'mongodb://localhost/{YOUR_DB_NAME}' );
var db = mongoose.connection;
function Animal(name) { | |
if (!(this instanceof Animal)) return new Animal(name); | |
this.name = name || 'unknown'; | |
} | |
module.exports = Animal; | |
Animal.prototype.feed = function(food) { | |
food = food || 'food'; | |
return 'Fed ' + this.name + ' some ' + food; | |
}; |
While this gist has been shared and followed for years, I regret not giving more background. It was originally a gist for the engineering org I was in, not a "general suggestion" for any React app.
Typically I avoid folders altogether. Heck, I even avoid new files. If I can build an app with one 2000 line file I will. New files and folders are a pain.
Can't share the complete code because the app's closed source and still in stealth mode, but here's how I'm using React Router and Redux in a large app with server rendering and code splitting on routes.
- Wildcard Express route configures a Redux store for each request and makes
an
addReducers()
callback available to thegetComponents()
method of each React Router route. Each route is responsible for adding any Redux reducers it needs when it's loaded. (This isn't really necessary on the