Permalink
Browse files

Support for '[]' as array specifier in HTTP params in node 0.3.x. '*'…

… prefix will continue to work.
  • Loading branch information...
1 parent 3f62c65 commit cb365fbb8c03017dfb27ac5645596fc5d96c6ed8 @cskr committed Dec 7, 2010
View
@@ -49,10 +49,14 @@ function update(props, converter, complete) {
}
keys.forEach(function(prop) {
var propVal = props[prop];
- if(Array.isArray(propVal) || prop.substring(0, 1) == '*') {
+ if(Array.isArray(propVal) || prop.substring(0, 1) == '*'
+ || prop.substring(prop.length - 2) == '[]') {
if(prop.substring(0, 1) == '*') {
var makeArray = true;
prop = prop.substring(1);
+ } else if(prop.substring(prop.length - 2) == '[]') {
+ var makeArray = true;
+ prop = prop.substring(0, prop.length - 2);
} else {
var breakArray = true;
}
@@ -9,8 +9,19 @@ function Person() {
gh.initModel(Person, 'name', 'address');
+function Country() {
+}
+
+gh.initModel(Country, 'name', 'cities');
+
gh.post('/people', function() {
var person = new Person().update(this.params['person'])
this.model['person'] = person;
- this.render('model_values');
+ this.render('person_view');
+});
+
+gh.post('/countries', function() {
+ var country = new Country().update(this.params['country']);
+ this.model['country'] = country;
+ this.render('country_view');
});
@@ -26,6 +26,34 @@ suite.tests = {
next();
});
});
+ },
+
+ 'Array values with a *.': function(next) {
+ testUtil.invoke('POST', '/countries', {}, 'country.name=India'
+ + '&country.*cities=Bangalore'
+ + '&country.*cities=Chennai',
+ function(res) {
+ res.on('data', function(chunk) {
+ assert.equal(chunk, 'India Bangalore Chennai\n');
+ next();
+ });
+ });
+ },
+
+ 'Array values with [].': function(next) {
+ if(process.version.split('.')[1] < 3) {
+ next();
+ return;
+ }
+ testUtil.invoke('POST', '/countries', {}, 'country.name=India'
+ + '&country.cities[]=Bangalore'
+ + '&country.cities[]=Chennai',
+ function(res) {
+ res.on('data', function(chunk) {
+ assert.equal(chunk, 'India Bangalore Chennai\n');
+ next();
+ });
+ });
}
}
@@ -0,0 +1 @@
+<%= country.name() %> <%= country.cities()[0] %> <%= country.cities()[1] %>

0 comments on commit cb365fb

Please sign in to comment.