Skip to content

Commit 1b7aa64

Browse files
committed
Merge pull request #113 from karlwestin/fix-78
Look ahead one step when building nested structures (#78)
2 parents 01c6a2d + 88591e9 commit 1b7aa64

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

backbone-nested.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
var fullPathLength = attrPath.length;
247247
var model = this;
248248

249-
Backbone.NestedModel.walkPath(newAttrs, attrPath, function(val, path){
249+
Backbone.NestedModel.walkPath(newAttrs, attrPath, function(val, path, next){
250250
var attr = _.last(path);
251251
var attrStr = Backbone.NestedModel.createAttrStr(path);
252252

@@ -301,7 +301,7 @@
301301

302302

303303
} else if (!val[attr]){
304-
if (_.isNumber(attr)){
304+
if (_.isNumber(next)){
305305
val[attr] = [];
306306
} else {
307307
val[attr] = {};
@@ -360,7 +360,7 @@
360360

361361
// walk through the child attributes
362362
for (var i = 0; i < attrPath.length; i++){
363-
callback.call(scope || this, val, attrPath.slice(0, i + 1));
363+
callback.call(scope || this, val, attrPath.slice(0, i + 1), attrPath[i + 1]);
364364

365365
childAttr = attrPath[i];
366366
val = val[childAttr];

test/nested-model.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,32 @@ $(document).ready(function() {
309309
deepEqual(json, doc.attributes);
310310
});
311311

312+
test("#toJSON() after an 1-N set using brackets", function() {
313+
var provider = new Klass({
314+
"fields[0].key": 'LOGIN'
315+
});
316+
equal(provider.get('fields[0].key'), 'LOGIN');
317+
318+
deepEqual(provider.toJSON(), {
319+
"fields": [{
320+
"key": "LOGIN"
321+
}]
322+
});
323+
});
324+
325+
test("#toJSON() after an 1-N set using dots", function() {
326+
var provider = new Klass({
327+
"fields.0.key": 'LOGIN2'
328+
});
329+
equal(provider.get('fields.0.key'), 'LOGIN2');
330+
331+
deepEqual(provider.toJSON(), {
332+
"fields": [{
333+
"key": "LOGIN2"
334+
}]
335+
});
336+
});
337+
312338

313339
// ----- CHANGE EVENTS --------
314340

0 commit comments

Comments
 (0)