Skip to content

Commit 48b8161

Browse files
author
Thomas Bell
committed
Working on getting parsing just right for json
1 parent 40fb916 commit 48b8161

File tree

2 files changed

+90
-23
lines changed

2 files changed

+90
-23
lines changed

src/angular-dragdrop.js

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,86 @@
5050
constructor = callbackName.match(/^[^.]+.\s*/)[0].slice(0, -1); // matching a string upto a dot to check ctrl as syntax
5151
constructor = scope[constructor] && typeof scope[constructor].constructor === 'function' ? constructor : null;
5252
self.joinedArguments = [];
53-
var argsArray = (args && args.split(',') || []).map(function(arg, index, array) {
54-
// We need to determine if we have a json object in the argument list
55-
var firstArg = arg[0],
56-
nextIndex = index + 1,
57-
isNthItem = nextIndex == array.length;
58-
59-
if (firstArg == "{" && !isNthItem) {
60-
var nextItem = array[nextIndex],
61-
lastChar = nextItem[nextItem.length - 1];
62-
63-
if (lastChar == "}") {
64-
self.joinedArguments.push(nextItem);
65-
var joinedArg = [arg, nextItem].join();
66-
return joinedArg;
53+
self.getLastIndexJSONElement = function (array){
54+
for (var i = 0; i < array.length; i++) {
55+
var ithEl = array[i];
56+
//If this is the closing json element
57+
if(ithEl[ithEl.length -1] == "}"){
58+
// If this is not the last element
59+
if(i < (array.length -1)){
60+
// then check the next element for the closing bracket
61+
var nextItem = array[i + 1];
62+
// If the next element does not have a closing bracket
63+
if(nextItem.indexOf("}") == -1){
64+
// then the current index is the last
65+
return i;
66+
}
67+
else{
68+
// Then we are not the last closing tail
69+
continue;
70+
}
71+
}
72+
else {
73+
// else return this index
74+
return i;
75+
}
76+
}
77+
};
78+
// if we get this far then we haven't found a closing jsonelement
79+
return -1;
80+
};
81+
82+
self.getArgs = function(args) {
83+
return (args && args.split(',') || []).map(function(arg, index, array) {
84+
// We need to determine if we have a json object in the argument list
85+
var firstArg = arg[0],
86+
nextIndex = index + 1,
87+
isNthItem = nextIndex == array.length;
88+
89+
if (firstArg == "{" && !isNthItem) {
90+
var nextItem = array[nextIndex],
91+
lastChar = nextItem[nextItem.length - 1];
92+
93+
if (lastChar == "}") {
94+
self.joinedArguments.push(nextItem);
95+
var joinedArg = [arg, nextItem].join();
96+
return joinedArg;
97+
} else {
98+
return arg;
99+
}
67100
} else {
68-
return arg;
69-
}
70-
} else {
71-
if (self.joinedArguments.indexOf(arg) == -1) {
72-
return arg;
101+
if (self.joinedArguments.indexOf(arg) == -1) {
102+
return arg;
103+
}
73104
}
74-
}
75-
}, self);
76-
105+
}, self);
106+
};
107+
var argsArray = self.getArgs(args);
108+
console.log("args: ", argsArray);
109+
//(args && args.split(',') || []).map(function(arg, index, array) {
110+
// // We need to determine if we have a json object in the argument list
111+
// var firstArg = arg[0],
112+
// nextIndex = index + 1,
113+
// isNthItem = nextIndex == array.length;
114+
115+
// if (firstArg == "{" && !isNthItem) {
116+
// var nextItem = array[nextIndex],
117+
// lastChar = nextItem[nextItem.length - 1];
118+
119+
// if (lastChar == "}") {
120+
// self.joinedArguments.push(nextItem);
121+
// var joinedArg = [arg, nextItem].join();
122+
// return joinedArg;
123+
// } else {
124+
// return arg;
125+
// }
126+
// } else {
127+
// if (self.joinedArguments.indexOf(arg) == -1) {
128+
// return arg;
129+
// }
130+
// }
131+
// }, self);
132+
77133
return {
78134
callback: callbackName.substring(constructor && constructor.length + 1 || 0, atStartBracket),
79135
args: argsArray.map(function(item) {

test/spec/tests.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,16 @@ describe('Service: ngDragDropService', function() {
189189
};
190190
var callbackName = "startDrag("+ expectedFirstArg +","+ JSON.stringify(expectedData) +")";
191191
ngDragDropService.callEventCallback(localScope, callbackName, {}, {});
192-
});
192+
});
193+
194+
it('should parse nested json objects as a single argument', function(){
195+
var localScope = rootScope.$new();
196+
var expectedData = {r1: 1, r2: {rs1: "someId", rs2: "someother"}};
197+
localScope.startDrag = function(event, ui, data){
198+
expect(data).toEqual(expectedData);
199+
};
200+
var callbackName = "startDrag("+ JSON.stringify(expectedData) +")";
201+
ngDragDropService.callEventCallback(localScope, callbackName, {}, {});
202+
});
203+
193204
});

0 commit comments

Comments
 (0)