Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support for JSON model attributes #143

Closed
wants to merge 2 commits into from

2 participants

@MikeAski

We can encounter cases when we need structured attributes which are embedded in a model, not related to.

Mike Andrzej... added some commits
@tomdale
Owner

We need to handle this in a more sophisticated way, because we feel that people might change the nested data and not having that dirty the parent record violates the principle of least surprise.

For more discussion, see #53.

@tomdale tomdale closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 7, 2012
  1. Add support for JSON model attributes

    Mike Andrzejewski authored
  2. Add tests on JSON model attributes support

    Mike Andrzejewski authored
This page is out of date. Refresh to see the latest.
View
10 packages/ember-data/lib/system/model/attributes.js
@@ -125,6 +125,16 @@ DS.attr.transforms = {
return null;
}
}
+ },
+
+ json: {
+ from: function(serialized) {
+ return Ember.none(serialized) ? serialized : JSON.parse(serialized);
+ },
+
+ to: function(deserialized) {
+ return Ember.none(deserialized) ? deserialized : JSON.stringify(deserialized);
+ }
}
};
View
20 packages/ember-data/tests/unit/model_test.js
@@ -134,6 +134,26 @@ test("a DS.Model can describe Date attributes", function() {
convertsWhenSet('date', date, dateString);
});
+test("a DS.Model can describe JSON attributes", function() {
+ converts('json', null, null);
+ converts('json', undefined, undefined);
+
+ var jsonString = "{\"key\":\"value\",\"x\":42}";
+ var json = JSON.parse(jsonString);
+
+ var model = DS.Model._create({
+ structField: DS.attr('json')
+ });
+
+ model.send('loadingData');
+ model.send('didChangeData');
+
+ model.set('structField', json);
+ deepEqual(json, get(model, 'structField'), "setting JSON returns the same JSON");
+ convertsFromServer('json', jsonString, json);
+ convertsWhenSet('json', json, jsonString);
+});
+
test("retrieving properties should return the same value as they would if they were not in the data hash if the record is not loaded", function() {
var store = DS.Store.create({
adapter: DS.Adapter.create({
Something went wrong with that request. Please try again.