Skip to content
This repository was archived by the owner on Feb 18, 2021. It is now read-only.

Commit 0800d1d

Browse files
committed
added new angular template for resource module
1 parent 22b8ebc commit 0800d1d

File tree

9 files changed

+485
-5
lines changed

9 files changed

+485
-5
lines changed

tools/helpers/grunt/lib/generate/questions.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ var outputAnswers = {
2626

2727
//---
2828

29-
// To assign on outputAnswers.values.helpers
30-
var commonHelpers = {
29+
var helpersSet = {
3130
capitalize: function(value) {
3231
return _s.capitalize(value);
32+
},
33+
34+
stringRegExpEscape: function(string) {
35+
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
3336
}
3437
};
3538

39+
40+
// To assign on outputAnswers.values.helpers
41+
var commonHelpers = {
42+
capitalize: helpersSet.capitalize
43+
};
44+
3645
//---
3746

3847
var templates = {
@@ -57,9 +66,16 @@ var templates = {
5766
source: '/angularjs/crud',
5867
destination: 'app', // concat with output destination
5968
helpers: {
60-
stringRegExpEscape: function(string) {
61-
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
62-
}
69+
stringRegExpEscape: helpersSet.stringRegExpEscape
70+
}
71+
},
72+
73+
'angularjs_resource': {
74+
type: 'directory',
75+
source: '/angularjs/resource',
76+
destination: 'app', // concat with output destination
77+
helpers: {
78+
stringRegExpEscape: helpersSet.stringRegExpEscape
6379
}
6480
}
6581

@@ -105,6 +121,10 @@ var questions = {
105121
name: 'Use Case (CRUD)',
106122
value: { type: 'template', key: 'angularjs_crud' }
107123
},
124+
{
125+
name: 'Resource',
126+
value: { type: 'template', key: 'angularjs_resource' }
127+
},
108128
{
109129
name: 'Page',
110130
value: { type: 'template', key: 'angularjs_page' }
@@ -319,6 +339,32 @@ var askFor = {
319339
return output;
320340
});
321341

342+
});
343+
},
344+
345+
angularjs_resource: function() {
346+
return askFor.name()
347+
.then(function(answer) {
348+
var output = {};
349+
350+
output.name = answer.input;
351+
352+
return output;
353+
})
354+
.then(function(output) {
355+
356+
return ask(inputQuestion(
357+
'input',
358+
'Define resource url:',
359+
outputAnswers.restContext + '/' + output.name
360+
))
361+
.then(function(answer) {
362+
output.endpoint = answer.input;
363+
})
364+
.then(function() {
365+
return output;
366+
});
367+
322368
});
323369
}
324370

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
TODO:
3+
4+
1 - update /src/require.mock.load.js
5+
6+
define(
7+
// require.js dependency injection
8+
[
9+
'shared/mock/require.load',
10+
11+
...
12+
13+
// ↓↓↓ ADD ↓↓↓
14+
'<%= location %>/mock/require.load'
15+
],
16+
17+
// require.js module scope
18+
function() {});
19+
20+
21+
2 - update /src/app/main/module.js
22+
23+
define(
24+
// require.js dependency injection
25+
[
26+
'angular',
27+
'angularRoute',
28+
29+
...
30+
31+
// ↓↓↓ ADD ↓↓↓
32+
'<%= location %>/require.load'
33+
],
34+
35+
// require.js module scope
36+
function(ng) {
37+
'use strict';
38+
39+
// Module definition
40+
return ng.module(
41+
42+
// module name
43+
'main',
44+
45+
// module dependencies
46+
[
47+
'ngRoute',
48+
49+
...
50+
51+
// ↓↓↓ ADD ↓↓↓
52+
'<%= name %>'
53+
]
54+
);
55+
56+
});
57+
58+
59+
3 - delete this file : README.txt
60+
61+
62+
-------------------------------------------------------------------------------
63+
64+
Values to templates:
65+
66+
name: <%= name %>
67+
68+
name capitalized: <%= helpers.capitalize( name ) %>
69+
70+
location: <%= location %>
71+
72+
backend resource: <%= endpoint %>
73+
74+
backend resource regexp escape: <%= helpers.stringRegExpEscape( endpoint ) %>
75+
76+
-------------------------------------------------------------------------------
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
define(
2+
// require.js dependency injection
3+
[
4+
'shared/mock/backend'
5+
],
6+
7+
// require.js module scope
8+
function(backend) {
9+
'use strict';
10+
11+
12+
backend.addResource(
13+
// mock resource dependencies injection
14+
['$httpBackend', 'regexpUrl',
15+
16+
// mock resource definition
17+
function($httpBackend, regexpUrl) {
18+
19+
//--- @begin: Allow pass to server
20+
21+
// get all
22+
$httpBackend
23+
.when('GET', regexpUrl(/<%= helpers.stringRegExpEscape( endpoint ) %>(\?|$)/))
24+
.passThrough();
25+
26+
// get one
27+
$httpBackend
28+
.when('GET', regexpUrl(/<%= helpers.stringRegExpEscape( endpoint ) %>(\/)?([A-z0-9]+)?$/))
29+
.passThrough();
30+
31+
// create
32+
$httpBackend
33+
.when('POST', regexpUrl(/<%= helpers.stringRegExpEscape( endpoint ) %>$/))
34+
.passThrough();
35+
36+
// update
37+
$httpBackend
38+
.when('PUT', regexpUrl(/<%= helpers.stringRegExpEscape( endpoint ) %>(\/)?([A-z0-9]+)?$/))
39+
.passThrough();
40+
41+
// delete
42+
$httpBackend
43+
.when('DELETE', regexpUrl(/<%= helpers.stringRegExpEscape( endpoint ) %>(\/)?([A-z0-9]+)?$/))
44+
.passThrough();
45+
46+
//--- @end: Allow pass to server
47+
}]);
48+
49+
50+
});
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
define(
2+
// require.js dependency injection
3+
[
4+
'shared/mock/module'
5+
],
6+
7+
// require.js module scope
8+
function(module) {
9+
'use strict';
10+
11+
12+
module.factory(
13+
14+
// factory name
15+
'<%= helpers.capitalize( name ) %>Collection',
16+
17+
// factory dependencies injection
18+
['DataStore', 'Helpers', '$log',
19+
20+
// factory definition
21+
function(DataStore, helpers, console) {
22+
23+
console.debug('<%= helpers.capitalize( name ) %>Collection');
24+
25+
/*
26+
options = {
27+
name: '',
28+
objType: '',
29+
indicesArray: ['id'],
30+
fn: {
31+
searchValue: function(data, find) {},
32+
init: function( collection ) {}
33+
}
34+
}
35+
*/
36+
var collection = DataStore.create({
37+
name: '<%= name %>',
38+
objType: '<%= helpers.capitalize( name ) %>',
39+
indicesArray: ['id', 'name'],
40+
fn: {
41+
searchValue: function(data, find) {
42+
43+
if(!find) return [];
44+
45+
var r = [], obj,
46+
regexp = new RegExp(find, 'i'),
47+
len = data.length;
48+
49+
for (var i = 0; i < len; i++) {
50+
obj = data[i];
51+
52+
if(obj.name.match(regexp) || obj.description.match(regexp))
53+
r.push(obj);
54+
}
55+
56+
return r;
57+
58+
},
59+
60+
init: function( collection ) {
61+
62+
console.debug( 'init <%= helpers.capitalize( name ) %>Collection' );
63+
64+
var seq = 0;
65+
66+
for (var i = 42; i > 0; i--) {
67+
collection.insert({
68+
id : seq,
69+
name : 'fake <%= helpers.capitalize( name ) %> name ' + (seq+1),
70+
description : 'some fake <%= helpers.capitalize( name ) %> descrition ' + (seq+1)
71+
});
72+
seq++;
73+
}
74+
75+
}
76+
}
77+
});
78+
79+
80+
function <%= helpers.capitalize( name ) %>Collection() {}
81+
var ClassDef = helpers.extendsFn( <%= helpers.capitalize( name ) %>Collection, collection );
82+
83+
ClassDef.prototype.sayMyName = function() {
84+
return 'Angular.js : <%= helpers.capitalize( name ) %>Collection Mock';
85+
};
86+
87+
var instance = new <%= helpers.capitalize( name ) %>Collection();
88+
89+
console.debug( instance.sayMyName() );
90+
console.debug( instance );
91+
console.debug( instance.list() );
92+
93+
return instance;
94+
95+
}]);
96+
97+
98+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
define(
2+
// require.js dependency injection
3+
[
4+
// allow request pass through angular.js mock url interceptor
5+
//'./allow-pass'
6+
7+
/**/
8+
'./data',
9+
'./url-interceptors'
10+
/**/
11+
],
12+
13+
// require.js module scope
14+
function() {});

0 commit comments

Comments
 (0)