Skip to content

Commit

Permalink
Merge pull request #48 from TheSharpieOne/master
Browse files Browse the repository at this point in the history
Refactoring, Tests, and More
  • Loading branch information
TheSharpieOne committed Feb 12, 2015
2 parents 7730996 + ef40dee commit 59cb59c
Show file tree
Hide file tree
Showing 14 changed files with 1,152 additions and 87 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ results

npm-debug.log
node_modules
bower_components
build

.tern-project
.tern-project
.idea
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
node_js:
- "0.10"

before_script:
- npm install
- npm install -g gulp
- npm install -g bower
- bower install -F

script:
- gulp build-js
- gulp test-build
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/janpantel/angular-sails.svg?branch=master)](https://travis-ci.org/janpantel/angular-sails)

Angular Sails
=============

Expand Down Expand Up @@ -28,15 +30,24 @@ app.controller("FooController", function ($scope, $sails) {
$scope.bars = [];

(function () {
// Using .success() and .error()
$sails.get("/bars")
.success(function (data) {
.success(function (data, status, headers, jwr) {
$scope.bars = data;
})
.error(function (data) {
.error(function (data, status, headers, jwr) {
alert('Houston, we got a problem!');
});

// Using .then()
$sails.get("/bars")
.then(function(resp){
$scope.bars = resp.data;
}, function(resp){
alert('Houston, we got a problem!');
});

// Watching for updates
$sails.on("bars", function (message) {
if (message.verb === "created") {
$scope.bars.push(message.data);
Expand Down
8 changes: 7 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
"license": "MIT",
"homepage": "https://github.com/kyjan/angular-sails",
"dependencies": {
"angular": ">=1.2.*"
"angular": ">=1.2.*",
"sails.io.js": "*"
},
"devDependencies":{
"angular-mocks": ">=1.2.*"
},
"ignore": [
"**/.*",
"gulpfile.js",
"package.json",
"node_modules",
"build",
"mock",
"bower_components",
"app/bower_components",
"test",
Expand Down
41 changes: 32 additions & 9 deletions dist/angular-sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ angular.module('ngSails').provider('$sails', function () {

this.url = undefined;
this.interceptors = [];
this.responseHandler = undefined;

this.$get = ['$q', '$timeout', function ($q, $timeout) {
var socket = io.connect(provider.url),
Expand All @@ -19,23 +20,45 @@ angular.module('ngSails').provider('$sails', function () {
promise = deferred.promise;

promise.success = function (fn) {
promise.then(fn);
promise.then(function(response) {
fn(response.data, response.status, response.headers);
});
return promise;
};

promise.error = function (fn) {
promise.then(null, fn);
promise.then(null, function(response) {
fn(response.data, response.status, response.headers);
});
return promise;
};

return deferred;
},
resolveOrReject = function (deferred, data) {
// Make sure what is passed is an object that has a status and if that status is no 2xx, reject.
if (data && angular.isObject(data) && data.status && Math.floor(data.status / 100) !== 2) {
deferred.reject(data);
resolveOrReject = this.responseHandler || function (deferred, response) {
var jwr = response;

// backward compatibility with older sails.io (no JWR)
if(!(response instanceof Object && response.constructor.name === "JWR")){
jwr = {
body: response,
headers: response.headers || {},
statusCode: response.statusCode || response.status
};
}

// angular $http returns the 'body' as 'data'.
jwr.data = jwr.body;

// angular $http returns the 'statusCode' as 'status'.
jwr.status = jwr.statusCode;

// TODO: map 'status'/'statusCode' to a 'statusText' to mimic angular $http

if (jwr.error) {
deferred.reject(jwr);
} else {
deferred.resolve(data);
deferred.resolve(jwr);
}
},
angularify = function (cb, data) {
Expand All @@ -52,8 +75,8 @@ angular.module('ngSails').provider('$sails', function () {
data = null;
}
deferred.promise.then(cb);
socket['legacy_' + methodName](url, data, function (result) {
resolveOrReject(deferred, result);
socket['legacy_' + methodName](url, data, function (emulatedHTTPBody, jsonWebSocketResponse) {
resolveOrReject(deferred, jsonWebSocketResponse || emulatedHTTPBody);
});
return deferred.promise;
};
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-sails.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var pkg = require('./package.json');

var pkgFiles = {
angular: [
'bower_components/angular/angular.js'
],
karma: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'mock/socket-io.js',
'bower_components/sails.io.js/sails.io.js'
],
'karma-build': [
'@karma',
'build/' + pkg.name + '.js',
'@karma-tests'
],
'karma-min': [
'@karma',
'build/' + pkg.name + '.min.js',
'@karma-tests'
],
'karma-src': [
'@karma',
'@src',
'@karma-tests'
],
'karma-tests': [
'test/**/*.spec.js'
],
src: [
'src/**/*.js',
]
};

if (module.exports) {
module.exports.files = pkgFiles;
module.exports.mergeFilesFor = function() {
var files = [];

Array.prototype.slice.call(arguments, 0).forEach(function(filegroup) {
pkgFiles[filegroup].forEach(function(file) {
// replace @ref
var match = file.match(/^\@(.*)/);
if (match) {
files = files.concat(pkgFiles[match[1]]);
} else {
files.push(file);
}
});
});

return files;
};
}
40 changes: 33 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
'use strict';

var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
header = require('gulp-header'),
footer = require('gulp-footer');
footer = require('gulp-footer'),
ngAnnotate = require('gulp-ng-annotate'),
gulpKarma = require('gulp-karma'),
pkg = require('./package.json'),
files = require('./files');

var karmaTestConfig = gulpKarma({configFile: 'karma.conf.js', action: 'run'});


gulp.task('build-js', function () {
gulp.src(['src/ngSails.js', 'src/**/*.js'])
.pipe(concat('angular-sails.js'))
return gulp.src(files.mergeFilesFor('src'))
.pipe(ngAnnotate())
.pipe(concat(pkg.name+'.js'))
.pipe(header('(function (angular, io) {\n\'use strict\''))
.pipe(footer('}(angular, io));'))
.pipe(gulp.dest('./dist/'))
.pipe(concat('angular-sails.min.js'))
.pipe(gulp.dest('./build/'))
.pipe(concat(pkg.name+'.min.js'))
.pipe(uglify())
.pipe(gulp.dest('./dist/'));
.pipe(gulp.dest('./build/'));
});

gulp.task('dist-js', ['build-js'], function () {
return gulp.src('./build/*.js')
.pipe(gulp.dest('./dist/'));
});

gulp.task('test', function () {
return gulp.src(files.mergeFilesFor('karma-src')).pipe(karmaTestConfig);
});

gulp.task('test-build', ['build-js'], function () {
return gulp.src(files.mergeFilesFor('karma-build')).pipe(karmaTestConfig);
});

gulp.task('test-min', ['build-js'], function () {
return gulp.src(files.mergeFilesFor('karma-min')).pipe(karmaTestConfig);
});

gulp.task('watch', function () {
gulp.watch('src/**/*.js', ['build-js']);
return gulp.watch('src/**/*.js', ['build-js']);
});

gulp.task('default', ['build-js', 'watch']);
38 changes: 38 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Karma configuration
// Generated on Thu Jan 09 2014 13:59:16 GMT-0500 (EST)

module.exports = function (config) {
config.set({
basePath: './',

frameworks: ['mocha', 'chai-sinon'],

exclude: [],

reporters: ['progress'],

port: 9876,

runnerPort: 9100,

colors: true,

logLevel: config.LOG_INFO,

autoWatch: true,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary (has to be installed with `npm install karma-chrome-canary-launcher`)
// - Firefox (has to be installed with `npm install karma-firefox-launcher`)
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['PhantomJS'],

captureTimeout: 60000,

singleRun: false
});
};
81 changes: 81 additions & 0 deletions mock/socket-io.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// fix for phantomjs < 2.0
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}

var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP && oThis ? this : oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};

fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();

return fBound;
};
}

function io() {
var mockSocketObject = createMockSocketObject();
return mockSocketObject.connect();
}

function createMockSocketObject() {

var socket = {
on: function(ev, fn) {
(this._listeners[ev] = this._listeners[ev] || []).push(fn);
},
once: function(ev, fn) {
(this._raw._listeners[ev] = this._raw._listeners[ev] || []).push(fn);
fn._once = true;
},
emit: function(ev, data) {
if (this._listeners[ev]) {
var args = arguments;
this._listeners[ev].forEach(function(listener) {
if (listener._once) {
this.removeListener(ev, listener);
}
listener.apply(null, Array.prototype.slice.call(args, 1));
}.bind(this));
}
},
_listeners: {},
removeListener: function(ev, fn) {
if (fn) {
var index = this._listeners[ev].indexOf(fn);
if (index > -1) {
this._listeners[ev].splice(index, 1);
}
} else {
delete this._listeners[ev];
}
},
removeAllListeners: function(ev) {
if (ev) {
delete this._listeners[ev];
} else {
this._listeners = {};
}
},
disconnect: function() {
this.connected = false;
this.emit('disconnect');
},
connect: function() {
this.connected = true;
this.emit('connect');
return this;
}
};

return socket;
}
Loading

0 comments on commit 59cb59c

Please sign in to comment.