-
Notifications
You must be signed in to change notification settings - Fork 1
/
backbone.dml.js
130 lines (110 loc) · 4.32 KB
/
backbone.dml.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
*
* Backbone-DML v0.1
*
* Copyright (c) 2014 Venkatraman Ramamoorthy
*
* https://github.com/ramsunvtech/backbone.dml
* Licensed under the MIT License
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['underscore', 'backbone'], factory);
} else if (typeof exports !== 'undefined') {
// CommonJS / Node.js
module.exports = factory(require('underscore'), require('backbone'));
} else {
// globals
factory(_, Backbone);
}
}(function(_, Backbone) {
'use strict';
_.extend(Backbone.Model.prototype, {
select: function(statement) {
var attr = this.attributes,
isAttrStack = (_.isArray(attr)),
selected = (isAttrStack) ? [] : {};
// Check If the String has 'as' or return the Splitted Array.
function alias(givenString, isExist) {
var aliasArray = givenString.split(' as ');
if(isExist) return (aliasArray.length > 1) ? true : false;
return aliasArray;
}
// Check If the String has '.' or return the Splitted Array.
function dot(givenString, isExist) {
var dotArray = givenString.split('.');
if(isExist) return (dotArray.length > 1) ? true : false;
return dotArray;
}
// Check If the String has '[]' or return the matched string.
function arg(givenString, isExist) {
var argStack = givenString.match(/\[([^\]]+)]/), arg, givenArray;
if(isExist) return (_.isArray(argStack)) ? (!_.isUndefined(argStack[1])) ? true : false : false;
arg = argStack[1];
fieldName = givenString.replace(argStack[0], '');
givenArray = attr[fieldName];
// Check If its Number.
if(!_.isEmpty(givenArray[arg])) {
return givenArray[arg];
}
// Check If its String.
else if(_.isString(arg)) {
var condition = arg.split(' && '),
where = {},
whereValue, index;
for(index in condition) {
var condInfo = condition[index].split('=');
where[condInfo[0]] = condInfo[1];
}
whereValue = _.where(givenArray, where);
return (whereValue.length > 1) ? whereValue : whereValue[0];
}
}
// Get the Field Name Alias and their Value.
function getFields(fieldsList, attr) {
var selected = {},
index, fieldName, fieldAlias, fieldValue;
// Iterating each Fields.
for(index in fieldsList) {
var fieldInfo = (isFieldsArray) ? fieldsList[index].trim() :
(isFieldsObject) ? index.concat(' as ', fieldsList[index]) : '';
if(alias(fieldInfo, true)) {
var aliasInfo = alias(fieldInfo);
fieldName = aliasInfo[0];
fieldAlias = aliasInfo[1];
}
else {
fieldName = fieldAlias = fieldInfo;
}
if(!_.isUndefined(fieldName)) {
fieldValue = (!_.isUndefined(attr[fieldName])) ? attr[fieldName] : '';
}
if(dot(fieldName, true)) {
var dotField = dot(fieldName);
fieldValue = attr[dotField[0]][dotField[1]];
}
if(arg(fieldName, true)) {
var filteredList = arg(fieldName);
fieldValue = filteredList;
}
selected[fieldAlias] = fieldValue;
}
return selected;
}
// Split it, if the statement is String.
var fieldsList = (_.isString(statement)) ? statement.split(',') : statement,
isFieldsArray = (_.isArray(fieldsList)) ? true : false,
isFieldsObject = (_.isObject(fieldsList)) ? true : false,
isIterable = (isFieldsArray || isFieldsArray) ? true : false;
// Check If Attribute is Array.
if(!isAttrStack) {
if(isIterable) {
selected = getFields(fieldsList, attr);
}
}
return selected;
}
});
return Backbone;
}));