Skip to content

Commit

Permalink
feature: added new attribute "valueName" for setting value "apingResu…
Browse files Browse the repository at this point in the history
…lts"
  • Loading branch information
JohnnyTheTank committed Jan 23, 2016
1 parent 71e6c7c commit 6e6ee62
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apiNG",
"version": "1.0.3",
"version": "1.0.4",
"homepage": "https://github.com/JohnnyTheTank/apiNG",
"authors": [
"Jonathan Hornung <[email protected]>"
Expand Down
24 changes: 20 additions & 4 deletions dist/aping.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
@name: aping
@version: 1.0.3 (23-01-2016)
@version: 1.0.4 (23-01-2016)
@author: Jonathan Hornung <[email protected]>
@url: https://github.com/JohnnyTheTank/apiNG
@license: MIT
Expand All @@ -18,7 +18,8 @@ angular.module('jtt_aping')
});

}])
.directive('aping', ['apingDefaultSettings', 'apingUtilityHelper', '$templateRequest', '$compile', function (apingDefaultSettings, apingUtilityHelper, $templateRequest, $compile) {
.value("apingResults", {})
.directive('aping', ['apingResults', 'apingDefaultSettings', 'apingUtilityHelper', '$templateRequest', '$compile', function (apingResults, apingDefaultSettings, apingUtilityHelper, $templateRequest, $compile) {
return {
restrict: 'E',
replace: 'false',
Expand All @@ -31,7 +32,8 @@ angular.module('jtt_aping')
orderReverse: '@',
templateUrl: '@',
payloadJson: '@',
removeDoubles: '@'
removeDoubles: '@',
valueName: '@'
},
link: function (scope, element, attrs) {
$templateRequest(scope.templateUrl || apingDefaultSettings.templateUrl).then(function (html) {
Expand All @@ -56,6 +58,14 @@ angular.module('jtt_aping')
var orderReverse;
var orderBy;
var removeDoubles;
var valueName;


if (angular.isDefined($scope.valueName)) {
valueName = $scope.valueName;
} else {
valueName = undefined;
}

if (angular.isDefined($scope.items)) {
items = $scope.items;
Expand Down Expand Up @@ -120,7 +130,8 @@ angular.module('jtt_aping')
maxItems: maxItems,
orderBy: orderBy,
orderReverse: orderReverse,
removeDoubles: removeDoubles
removeDoubles: removeDoubles,
valueName: valueName
};
};

Expand Down Expand Up @@ -158,6 +169,11 @@ angular.module('jtt_aping')
if (appSettings.maxItems > -1 && $scope.results.length > appSettings.maxItems) {
$scope.results = $scope.results.splice(0, appSettings.maxItems);
}

if(angular.isDefined(appSettings.valueName)) {
apingResults[appSettings.valueName] = $scope.results;
}

$scope.$broadcast('apiNG.resultMerged');
};
this.apply = function () {
Expand Down
4 changes: 2 additions & 2 deletions dist/aping.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aping",
"version": "1.0.3",
"version": "1.0.4",
"description": "apiNG is an AngularJS directive that enables you to receive, aggregate, limit, order and display data from one or more sources. The complete setup is dead simple, just by adding data-attributes to your html",
"main": "dist/aping.min.js",
"scripts": {
Expand Down
22 changes: 19 additions & 3 deletions src/aping-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ angular.module('jtt_aping')
});

}])
.directive('aping', ['apingDefaultSettings', 'apingUtilityHelper', '$templateRequest', '$compile', function (apingDefaultSettings, apingUtilityHelper, $templateRequest, $compile) {
.value("apingResults", {})
.directive('aping', ['apingResults', 'apingDefaultSettings', 'apingUtilityHelper', '$templateRequest', '$compile', function (apingResults, apingDefaultSettings, apingUtilityHelper, $templateRequest, $compile) {
return {
restrict: 'E',
replace: 'false',
Expand All @@ -21,7 +22,8 @@ angular.module('jtt_aping')
orderReverse: '@',
templateUrl: '@',
payloadJson: '@',
removeDoubles: '@'
removeDoubles: '@',
valueName: '@'
},
link: function (scope, element, attrs) {
$templateRequest(scope.templateUrl || apingDefaultSettings.templateUrl).then(function (html) {
Expand All @@ -46,6 +48,14 @@ angular.module('jtt_aping')
var orderReverse;
var orderBy;
var removeDoubles;
var valueName;


if (angular.isDefined($scope.valueName)) {
valueName = $scope.valueName;
} else {
valueName = undefined;
}

if (angular.isDefined($scope.items)) {
items = $scope.items;
Expand Down Expand Up @@ -110,7 +120,8 @@ angular.module('jtt_aping')
maxItems: maxItems,
orderBy: orderBy,
orderReverse: orderReverse,
removeDoubles: removeDoubles
removeDoubles: removeDoubles,
valueName: valueName
};
};

Expand Down Expand Up @@ -148,6 +159,11 @@ angular.module('jtt_aping')
if (appSettings.maxItems > -1 && $scope.results.length > appSettings.maxItems) {
$scope.results = $scope.results.splice(0, appSettings.maxItems);
}

if(angular.isDefined(appSettings.valueName)) {
apingResults[appSettings.valueName] = $scope.results;
}

$scope.$broadcast('apiNG.resultMerged');
};
this.apply = function () {
Expand Down
35 changes: 26 additions & 9 deletions src/aping-directive.spec.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
describe('A apingDefaultSettings Service', function(){
describe('A apingDefaultSettings Service', function () {

var $compile;
var $rootScope;
var $q;

// Mocks & Stubs
var templateRequestMock = function(){
return $q.when('<template>Test</template>')
var templateRequestMock = function () {
return $q.when('<div><pre>{{results | json}}</pre></div>')
};

beforeEach(module('jtt_aping', function($provide){
beforeEach(module('jtt_aping', function ($provide) {
// Mock apingDefaultSettings with what ever you want
// Do the same for other services if needed
$provide.value('apingDefaultSettings',{});
$provide.value('$templateRequest',templateRequestMock)
$provide.value('apingDefaultSettings', {
apingApiKeys: {}
});
$provide.value('$templateRequest', templateRequestMock)
}));

beforeEach(inject(function(_$compile_, _$rootScope_, _$q_){
beforeEach(inject(function (_$compile_, _$rootScope_, _$q_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$q = _$q_;
}));


it('should init scope.results with an empty array', function(){
it('should init scope.results with an empty array', function () {
var html = '<aping></aping>';

var element = $compile(html)($rootScope);


// TODO: Try to Debug the element and look "inside"
expect(element.isolateScope().results).toEqual([]);
});


it('should get an json file and merge it into results', function () {

var jsonloaderRequest = "{'path':'https://github.com/JohnnyTheTank/apiNG/blob/master/package.json'}";

var html = '<aping aping-jsonloader="['+jsonloaderRequest+']"></aping>';

var element = $compile(html)($rootScope);

waitFor(function () {
expect(element.isolateScope().results.length === 1);
}, "Results array length should be one", 5000);

});


});

0 comments on commit 6e6ee62

Please sign in to comment.