Skip to content

Instantly share code, notes, and snippets.

View philmander's full-sized avatar

Phil Mander philmander

View GitHub Profile
// movies/movie-lister.js
var MovieLister = function(movieFinder) {
this.movieFinder = movieFinder;
};
MovieLister.prototype.showMovies = function(query) {
var keywords = query.split(" ");
var movieResult = this.movieFinder.getMovies(keywords);
@philmander
philmander / inverted-inject-context-1.js
Last active December 11, 2015 00:09
Demonstrates property injecting the appContext into a proto instance
// movies/move-finder.js
define(["jquery"], function($) {
var MovieFinder = function() {
var self = this;
$(document).on("click", ".open-prefs", function() {
self.openPreferences();
});
};
MovieFinder.prototype.openPreferences = function() {
@philmander
philmander / inverted-interface-1.js
Last active December 11, 2015 00:09
Demonstrates using an interface in the app config
//app-config.js
define(function() {
return {
protos: {
movieLister: {
module: "movies/movie-lister",
args: [
//injects a json movie finder which must implement the movieFinder interface
"*jsonMovieFinder [movieFinder]"
]
//app-config.js
define(function() {
return {
protos: {
movieLister: {
module: "movies/movie-lister",
args: [
"*movieFinder"
]
mixin: {
// Mixed.js
define(function() {
var Mixed = function(a, b) {
this.a = a;
this.b = b;
};
Mixed.prototype.getA = function() {
return this.a;
};
Mixed.prototype.getB = function() {
@philmander
philmander / casper.fetchMicrodata.js
Last active August 29, 2015 13:58
Microdata util for CasperJS
//microdata polyfill (see https://github.com/termi/Microdata-JS)
casper.options.clientScripts.push("../../../support/a.js");
casper.options.clientScripts.push("../../../support/microdata-js.js");
/**
* Fetches microdata from the remote DOM environment, in a json object structure.
* @param itemType The item type of item scopes to fetch data for
* @returns {Object|mixed}
*/
casper.fetchMicrodata = function(itemType) {
@philmander
philmander / casper.test.assertMicrodata.js
Last active August 29, 2015 13:58
Assert Microdata test util for Casper JS. Requires https://gist.github.com/philmander/10012466
/**
* Asserts a microdata item value matches the given expected value in the remote DOM environment
* Depends on the casper.fetchMicrodata method
*
* @param itemType {String} The item type of the item scopes to look for properties in
* @param property {String} The name of the property the compare the expected value against
* @param expectedItemValue {String} The expected value of the item property
* @param message {String} The assertion message
* @param itemTypeIndex {Number} A specific index of an item scope to use. If omitted or null, will look for matches in
* all item scopes
@philmander
philmander / gulpdir
Created July 3, 2014 13:36
Litlle function for concatenating file names in gulp
//little util to do tidier directory concatenation
function dir() {
var args = Array.prototype.slice.call(arguments);
var prefix = "";
if(args[0] === "!") {
prefix = args[0];
args.shift();
}
return prefix + args.join("/");
}
@philmander
philmander / reduce-array-to-html.js
Last active August 29, 2015 14:12
Object array to html string with Array.reduce()
var items = [{
html: '<p>One</p>'
}, {
html: '<p>Two</p>'
}, {
html: '<p>Three</p>'
}];
var html = items.reduce(function(html, item) {
html.push(item.html);
@philmander
philmander / casperjs-website-resolution-tester.js
Created October 4, 2015 16:30
CasperJS Website Resolution Tester
var casper = require('casper').create({
verbose: true,
logLevel: 'info'
});
var page = require('webpage').create();
var utils = require('utils');
//args
var base = casper.cli.get('out');
var width = parseInt(casper.cli.get("width"));