-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
93 lines (84 loc) · 2.45 KB
/
plugin.js
File metadata and controls
93 lines (84 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Url alias feature main file
*/
const Alias = require('./lib/Alias.js');
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
plugin.setConfigs({
// we.js url alias feature
enableUrlAlias: true,
router: {
alias: {
// dont load alias for this routes
excludePaths: [ '/public/', '/favicon.ico', '/admin' ]
}
}
});
plugin.setResource({
'name': 'url-alia',
'findAll': {
'search': {
'alias': {
'parser': 'contains',
'target': {
'type': 'field',
'field': 'alias'
}
},
'target': {
'parser': 'contains',
'target': {
'type': 'field',
'field': 'target'
}
}
}
}
});
plugin.setModelUrlAliasFeatures = function setModelUrlAliasFeatures(we, done) {
for (let modelName in we.db.modelsConfigs) {
if (we.router.alias.modelHaveUrlAlias(we.db.modelsConfigs[modelName])) {
// add url alias virtual field
// we.db.modelsConfigs[modelName].definition.urlPath = {
// type: we.db.Sequelize.VIRTUAL,
// formFieldType: null,
// get: function() {
// if (this.cachedUrlPathAlias) return this.cachedUrlPathAlias;
// this.cachedUrlPathAlias = this.getUrlPathAlias()
// return this.cachedUrlPathAlias;
// }
// };
// field for set alias
we.db.modelsConfigs[modelName].definition.setAlias = {
type: we.db.Sequelize.VIRTUAL,
formFieldType: null
};
// add url alias hooks in all models if enable url alias
we.db.models[modelName].addHook('afterCreate',
'createUrlAlias'+modelName,
we.router.alias.afterCreatedRecord.bind({
we: we
})
);
we.db.models[modelName].addHook('afterUpdate',
'updateUrlAlias'+modelName,
we.router.alias.afterUpdatedRecord.bind({
we: we
})
);
we.db.models[modelName].addHook('afterDestroy',
'destroyUrlAlias'+modelName,
we.router.alias.afterDeleteRecord.bind({
we: we
})
);
}
}
done();
};
plugin.events.on('we:after:load:plugins', function (we) {
we.router.alias = new Alias(we);
});
plugin.hooks.on('we:models:set:joins', plugin.setModelUrlAliasFeatures);
return plugin;
};