Skip to content

Commit

Permalink
Support for '[]' as array specifier in HTTP params in node 0.3.x. '*'…
Browse files Browse the repository at this point in the history
… prefix will continue to work.
  • Loading branch information
cskr committed Dec 7, 2010
1 parent 3f62c65 commit cb365fb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
6 changes: 5 additions & 1 deletion grasshopper/lib/model.js
Expand Up @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion test/system/model_update/test-routes.js
Expand Up @@ -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');
});
28 changes: 28 additions & 0 deletions test/system/model_update/test.js
Expand Up @@ -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();
});
});
}
}

Expand Down
1 change: 1 addition & 0 deletions test/system/model_update/views/country_view.html
@@ -0,0 +1 @@
<%= country.name() %> <%= country.cities()[0] %> <%= country.cities()[1] %>

0 comments on commit cb365fb

Please sign in to comment.