Skip to content

Instantly share code, notes, and snippets.

@daffl
daffl / readme.md
Created October 4, 2013 21:45
Get started with CanJS, Bower and RequireJS

CanJS is one of the few client side MVC frameworks that can be used entirely modular. This means that if you only need can.Model you will only get the code necessary for can.Model. You can either use the download builder and select the components you want, clone the repository directly and use StealJS or use the AMD version from the full download or the Bower component.

Install CanJS

bower install canjs

Configure RequireJS

@daffl
daffl / router.js
Created November 18, 2013 20:27
Routing control with replacements
can.Control.extend({
setControl: function(Ctrl, options) {
if(this.current) {
this.current.destroy();
}
this.current = new Ctrl(this.element, options || this.options);
},
route: function() {
@daffl
daffl / Gruntfile.js
Created December 6, 2013 20:43
Example configuration for RequireJS and can-compile
cancompile: {
dist: {
src: ['public/app/**/*.mustache'],
out: 'public/dist/views.production.js',
wrapper: 'define([\'can/view/mustache\'], function(can) {\n{{{content}}}\n});'
}
},
requirejs: {
compile: {
options: {
@daffl
daffl / index.html
Created December 9, 2013 22:49
RequireJS development index.html configuration
<script src="components/requirejs/require.js"></script>
<script type="text/javascript">
require.config({
paths : {
jquery : "components/jquery/jquery",
can: "components/canjs/amd/can",
modal: "components/bootstrap/js/modal",
lodash: "components/lodash/dist/lodash"
},
shim: {
@daffl
daffl / can-jquery-model-json.js
Created February 28, 2014 17:24
A prefilter that allows to send your CanJS model as JSON
can.ajaxPrefilter(function(options, originalOptions) {
// Change ContentType to application/json for POST and PUT
if(options.processData && !(/^application\/json((\+|;).+)?$/i.test(options.contentType)) && /^(post|put|delete)$/i.test(options.type)) {
options.contentType = "application/json";
options.data = JSON.stringify(originalOptions.data);
}
});
@daffl
daffl / memory.js
Created June 5, 2014 22:44
Memory service implementation
'use strict';
// TODO eventually swap this out for the actual https://github.com/feathersjs/feathers-memory
var Proto = require('uberproto');
var _ = require('lodash');
// TODO (EK): Should we allow the ability to specify ascending
// or descending order for sort and/or order functions? Should
// you be able to sort by multiple attributes?
//
@daffl
daffl / application.js
Created June 12, 2014 18:59
can.Application concept
can.Application.extend({
template: can.view('index.stache'),
content: '#content',
state: {
},
routes: {
empty: function(data) {
@daffl
daffl / 2014-08-01-canjs-feathers-mongodb.markdown
Created July 31, 2014 19:15
CanJS + Feathers + MongoDB + Node

In this post I'd like to introduce a full JavaScript web application stack that uses MongoDB and Node on the server and CanJS on the client. The concept is similar to the MEAN (MongoDB, Express, Angular and Node) stack but has some advantages worth mentioning:

  1. The server boilerplate is small enough to not require a generator
  2. Thanks to Feathers you will get real-time capabilities for free
  3. You will use CanJS on the client

Feathers

@daffl
daffl / bower.json
Last active April 20, 2017 12:14
Mocha + FuncUnit
{
"name": "funcunit-dmeo",
"version": "0.0.0",
"main": "lib/index.js",
"license": "ISC",
"private": true,
"devDependencies": {
"mocha": "~1.15.0",
"chai": "~1.8.1",
"funcunit": "git://github.com/bitovi/funcunit.git#abeedf7e7e75445e0ef6bb82a8cef250664d4c67"
var userService = mongodb({
db: 'feathers-demo',
collection: 'users'
}).extend({
authenticate: function(username, password, callback) {
// This will be used as the MongoDB query
var query = {
username: username
};