Skip to content

Instantly share code, notes, and snippets.

@mallim
mallim / unit.js
Last active December 18, 2015 21:48 — forked from trek/unit.js
This code has been tested to work with casperjs 1.0.2 and phantomjs 1.9.1. After writing this, then I managed to get mocha-phantomjs (https://github.com/metaskills/mocha-phantomjs) to work and I switch to that. Primarily because mocha-phantomjs allows the usage of mocha's reporter straight without further customization. Additional things feature…
// Check for phantom
if ( !phantom.casperLoaded ) {
console.log( 'This script must be invoked using the casperjs executable' );
phantom.exit( 1 );
}
// get a Casper object.
// See http://casperjs.org/
// Instantiate casper
var casper = require('casper').create({
@mallim
mallim / preload.js
Created September 6, 2013 11:01
How to load external template in marionette if not using requirejs? Get the code from http://johndavidmathis.wordpress.com/2012/09/13/preloading-backbone-marionette-templates-2/
// Take note to change the source code for the correct path
// Example of using when there are multiple templates
var preloading = Backbone.Marionette.TemplateCache.preloadTemplates(['template1','template2'], this);
$.when(preloading).done(function() {
// Do something
});
@mallim
mallim / json_parse_eg.js
Created September 6, 2013 11:24
How to get a string from java backend and pass it to backbone model or collection?
// Within a jsp page
<script>
var options.saved_values=JSON.parse('${it.json_string_from_Java}');
var theBackboneModel = new TheBackboneModel(options.saved_values,{parse:true});
</script>
@mallim
mallim / jersey_jackson.java
Created September 6, 2013 11:29
Jersey + Jackson can map to a String and a Map<String,Object>
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/some_path")
@mallim
mallim / web.xml
Created September 6, 2013 11:38
Example of the setup in order to use Jersey1+Spring+Jackson 2 in the root path
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true"
>
<display-name>Archetype Created Web Application</display-name>
@mallim
mallim / express_example.js
Created September 6, 2013 11:41
Code sample to run express (with Handlebars as the view engine)
var express = require('express');
var cons = require('consolidate');
var http = require('http');
var reload = require('reload');
var app = express();
var server = http.createServer(app);
// Assign the handlebars engine to .html files
app.engine('html', cons.handlebars);
@mallim
mallim / 1.js
Created September 10, 2013 01:13
requirejs - Have a module that takes no dependencies. Example from http://tech.pro/blog/1561/five-helpful-tips-when-using-requirejs
//
// Have a module that takes no dependencies.
//
define({
someProp: "Oooh, how interesting!",
someMethod: function() {
// do interesting work
return compellingValue;
}
});
@mallim
mallim / TestStandaloneStories.java
Created October 9, 2013 01:47
JBehave with Spring Framework Setup - Advantage of this approach - can control the story sequence
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.configuration.spring.SpringStoryControls;
import org.jbehave.core.failures.FailingUponPendingStep;
import org.jbehave.core.failures.PendingStepStrategy;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
@mallim
mallim / TestWebStories.java
Created October 9, 2013 01:49
JBehave, Selenium with Spring
import com.thoughtworks.selenium.Selenium;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.embedder.StoryControls;
import org.jbehave.core.failures.FailingUponPendingStep;
import org.jbehave.core.failures.PendingStepStrategy;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.reporters.CrossReference;
@mallim
mallim / MyModelOverrideLocal.js
Created October 18, 2013 06:53
Example showing how to override Backbone Model sync method (specific to a model)
var MyModelOverrideLocal = Backbone.Model.extend({
sync: function (method, model, options) {
if ( method === 'delete' ) {
if ( options.data ) {
// properly formats data for back-end to parse
options.data = JSON.stringify(options.data);
}
// transform all delete requests to application/json