Skip to content

Commit

Permalink
Merge pull request #79 from kiva/pre-release
Browse files Browse the repository at this point in the history
Pre release
  • Loading branch information
Gabriel committed May 14, 2015
2 parents 5f9e524 + 5a95beb commit a6d15da
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bower.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backbone.siren",
"version": "0.3.6",
"version": "0.3.7",
"dependencies": {
"backbone": "~1.1.0",
"jquery": "~2.0.0",
Expand Down
39 changes: 36 additions & 3 deletions dist/amd/backbone.siren.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Backbone.Siren v0.3.6
* Backbone.Siren v0.3.7
*
* Copyright (c) 2014 Kiva Microfunds
* Copyright (c) 2015 Kiva Microfunds
* Licensed under the MIT license.
* https://github.com/kiva/backbone.siren/blob/master/license.txt
*/
Expand Down Expand Up @@ -216,6 +216,23 @@ define(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {
}


/**
* Given a mapping of roots to arrays of paths, swap them.
*
* @param {Object} roots
* @returns {Object}
*/
function mapRoots(roots) {
var mapped = {};
_.each(roots, function(paths, key) {
_.each(paths, function(path) {
mapped[path] = key;
});
});
return mapped;
}


/**
* Access to the entity's "actions"
*
Expand Down Expand Up @@ -1199,9 +1216,12 @@ define(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {
* @param {Object} options
*/
init: function (apiRoot, options) {
options = options || {};

this.apiRoot = apiRoot;
this.options = options;
this.isAbsoluteRegExp = new RegExp('^(?:[a-z]+:)?//', 'i');
this.alternateRoots = mapRoots(options.alternateRoots);
}


Expand All @@ -1216,7 +1236,20 @@ define(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {
return entityPath;
}

return this.apiRoot + '/' + entityPath;
return this.getRootForPath(entityPath) + '/' + entityPath;
}


/**
* Get the api root for the given path. If a root is not specified, returns the default root.
*
* @param {String} path
* @returns {String}
*/
, getRootForPath: function (path) {
// remove parameters an anchor tags from path
var strippedPath = path.split(/[\?#]/)[0];
return this.alternateRoots[strippedPath] ? this.alternateRoots[strippedPath] : this.apiRoot;
}


Expand Down
6 changes: 3 additions & 3 deletions dist/amd/backbone.siren.min.js

Large diffs are not rendered by default.

39 changes: 36 additions & 3 deletions dist/iife/backbone.siren.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Backbone.Siren v0.3.6
* Backbone.Siren v0.3.7
*
* Copyright (c) 2014 Kiva Microfunds
* Copyright (c) 2015 Kiva Microfunds
* Licensed under the MIT license.
* https://github.com/kiva/backbone.siren/blob/master/license.txt
*/
Expand Down Expand Up @@ -216,6 +216,23 @@
}


/**
* Given a mapping of roots to arrays of paths, swap them.
*
* @param {Object} roots
* @returns {Object}
*/
function mapRoots(roots) {
var mapped = {};
_.each(roots, function(paths, key) {
_.each(paths, function(path) {
mapped[path] = key;
});
});
return mapped;
}


/**
* Access to the entity's "actions"
*
Expand Down Expand Up @@ -1199,9 +1216,12 @@
* @param {Object} options
*/
init: function (apiRoot, options) {
options = options || {};

this.apiRoot = apiRoot;
this.options = options;
this.isAbsoluteRegExp = new RegExp('^(?:[a-z]+:)?//', 'i');
this.alternateRoots = mapRoots(options.alternateRoots);
}


Expand All @@ -1216,7 +1236,20 @@
return entityPath;
}

return this.apiRoot + '/' + entityPath;
return this.getRootForPath(entityPath) + '/' + entityPath;
}


/**
* Get the api root for the given path. If a root is not specified, returns the default root.
*
* @param {String} path
* @returns {String}
*/
, getRootForPath: function (path) {
// remove parameters an anchor tags from path
var strippedPath = path.split(/[\?#]/)[0];
return this.alternateRoots[strippedPath] ? this.alternateRoots[strippedPath] : this.apiRoot;
}


Expand Down
6 changes: 3 additions & 3 deletions dist/iife/backbone.siren.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backbone.siren",
"version": "0.3.6",
"version": "0.3.7",
"description": "Converts Siren JSON representations into Backbone Models and Collections",
"main": "index.js",
"scripts": {
Expand Down
35 changes: 34 additions & 1 deletion src/backbone.siren.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ function url() {
}


/**
* Given a mapping of roots to arrays of paths, swap them.
*
* @param {Object} roots
* @returns {Object}
*/
function mapRoots(roots) {
var mapped = {};
_.each(roots, function(paths, key) {
_.each(paths, function(path) {
mapped[path] = key;
});
});
return mapped;
}


/**
* Access to the entity's "actions"
*
Expand Down Expand Up @@ -1190,9 +1207,12 @@ BbSiren.prototype = {
* @param {Object} options
*/
init: function (apiRoot, options) {
options = options || {};

this.apiRoot = apiRoot;
this.options = options;
this.isAbsoluteRegExp = new RegExp('^(?:[a-z]+:)?//', 'i');
this.alternateRoots = mapRoots(options.alternateRoots);
}


Expand All @@ -1207,7 +1227,20 @@ BbSiren.prototype = {
return entityPath;
}

return this.apiRoot + '/' + entityPath;
return this.getRootForPath(entityPath) + '/' + entityPath;
}


/**
* Get the api root for the given path. If a root is not specified, returns the default root.
*
* @param {String} path
* @returns {String}
*/
, getRootForPath: function (path) {
// remove parameters an anchor tags from path
var strippedPath = path.split(/[\?#]/)[0];
return this.alternateRoots[strippedPath] ? this.alternateRoots[strippedPath] : this.apiRoot;
}


Expand Down
43 changes: 40 additions & 3 deletions test/spec/backbone.siren.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,15 @@ describe('Backbone.Siren: ', function () {
// Prototype methods
//

describe('.init', function () {
describe('.init()', function () {
var sirenApi = new Backbone.Siren('http://blah.io', {'ba': 'boom'});
sirenApi.init('http://new.io', {'ba': 'bing'});
expect(sirenApi.apiRoot).toBe('http://new.io');
expect(sirenApi.options).toEqual({'ba': 'bing'});
});


describe('.entityPathToUrl', function () {
describe('.entityPathToUrl()', function () {
it('turns an entity path into a full url', function () {
var sirenApi = new Backbone.Siren('http://blah.io');
expect(sirenApi.entityPathToUrl('some/path')).toBe('http://blah.io/some/path');
Expand All @@ -571,7 +571,44 @@ describe('Backbone.Siren: ', function () {
});


describe('.resolve', function () {
describe('.getRootForPath()', function() {
it('strips the parameters from the path when checking for root path', function() {
var sirenApi = new Backbone.Siren('/api');
sirenApi.alternateRoots = {
'/my/route': '/right'
,'/my/route?params': '/wrong'
,'/my/route#anchor': '/also-wrong'
,'/my/route?params#anchor': '/you-are-never-going-to-pass-this-test'
};

expect(sirenApi.getRootForPath('/my/route?params')).toBe('/right');
expect(sirenApi.getRootForPath('/my/route#anchor')).toBe('/right');
expect(sirenApi.getRootForPath('/my/route?params#anchor')).toBe('/right');
});


it('returns the default root when no alternate root is found for the path', function() {
var sirenApi = new Backbone.Siren('/right');
sirenApi.alternateRoots = {
'/my/other-route': '/wrong'
};

expect(sirenApi.getRootForPath('/my/route')).toBe('/right');
});


it('returns the alternate root for the path', function() {
var sirenApi = new Backbone.Siren('/wrong');
sirenApi.alternateRoots = {
'/my/route': '/right'
};

expect(sirenApi.getRootForPath('/my/route')).toBe('/right');
});
});


describe('.resolve()', function () {
beforeEach(function () {
this.stub(Backbone.Siren, 'resolve');
});
Expand Down

0 comments on commit a6d15da

Please sign in to comment.