Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added toFlatJSON method. #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions distribution/deep-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
factory(_, Backbone);
}
}(function(_, Backbone) {

/**
* Takes a nested object and returns a shallow object keyed with the path names
* e.g. { "level1.level2": "value" }
Expand Down Expand Up @@ -184,7 +184,7 @@
if (result == null && i < n - 1) {
result = {};
}

if (typeof result === 'undefined') {
if (return_exists)
{
Expand Down Expand Up @@ -263,6 +263,11 @@
return _.deepClone(this.attributes);
},

// Returns a flat representation of the model, deep properties are returned as dotted paths.
toFlatJSON: function () {
return objToPaths(this.attributes);
},

// Override get
// Supports nested attributes via the syntax 'obj.attr' e.g. 'author.user.name'
get: function(attr) {
Expand All @@ -274,7 +279,7 @@
set: function(key, val, options) {
var attr, attrs, unset, changes, silent, changing, prev, current;
if (key == null) return this;

// Handle both `"key", value` and `{key: value}` -style arguments.
if (typeof key === 'object') {
attrs = key;
Expand All @@ -284,7 +289,7 @@
}

options || (options = {});

// Run validation.
if (!this._validate(attrs, options)) return false;

Expand Down Expand Up @@ -329,11 +334,15 @@

//<custom code>
var separator = DeepModel.keyPathSeparator;
var alreadyTriggered = {}; // * @restorer

for (var i = 0, l = changes.length; i < l; i++) {
var key = changes[i];

this.trigger('change:' + key, this, getNested(current, key), options);
if (!alreadyTriggered.hasOwnProperty(key) || !alreadyTriggered[key]) { // * @restorer
alreadyTriggered[key] = true; // * @restorer
this.trigger('change:' + key, this, getNested(current, key), options);
} // * @restorer

var fields = key.split(separator);

Expand All @@ -342,7 +351,17 @@
var parentKey = _.first(fields, n).join(separator),
wildcardKey = parentKey + separator + '*';

this.trigger('change:' + wildcardKey, this, getNested(current, parentKey), options);
if (!alreadyTriggered.hasOwnProperty(wildcardKey) || !alreadyTriggered[wildcardKey]) { // * @restorer
alreadyTriggered[wildcardKey] = true; // * @restorer
this.trigger('change:' + wildcardKey, this, getNested(current, parentKey), options);
} // * @restorer

// + @restorer
if (!alreadyTriggered.hasOwnProperty(parentKey) || !alreadyTriggered[parentKey]) {
alreadyTriggered[parentKey] = true;
this.trigger('change:' + parentKey, this, getNested(current, parentKey), options);
}
// - @restorer
}
//</custom code>
}
Expand Down Expand Up @@ -388,7 +407,7 @@
//</custom code>

var old = this._changing ? this._previousAttributes : this.attributes;

//<custom code>
diff = objToPaths(diff);
old = objToPaths(old);
Expand Down Expand Up @@ -431,7 +450,7 @@

//For use in NodeJS
if (typeof module != 'undefined') module.exports = DeepModel;

return Backbone;

}));
Expand Down
2 changes: 1 addition & 1 deletion distribution/deep-model.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/deep-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@
return _.deepClone(this.attributes);
},

// Returns a flat representation of the model, deep properties are returned as dotted paths.
toFlatJSON: function () {
return objToPaths(this.attributes);
},

// Override get
// Supports nested attributes via the syntax 'obj.attr' e.g. 'author.user.name'
get: function(attr) {
Expand Down
13 changes: 13 additions & 0 deletions test/deep-model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,3 +941,16 @@ test('set: Trigger model change:[attribute] event for parent keys (like wildcard
})();
});
// - @restorer


test("toFlatJSON: Returns a flat representation of the model (deep properties are returned as dotted paths)", function() {
var model = create();

deepEqual(model.toFlatJSON(), {
id: 123,
'user.type': 'Spy',
'user.name.first': 'Sterling',
'user.name.last': 'Archer'
});

});