Skip to content

Instantly share code, notes, and snippets.

@orestis
Last active May 4, 2016 16:19
Show Gist options
  • Save orestis/f57e4bff221997a87f51 to your computer and use it in GitHub Desktop.
Save orestis/f57e4bff221997a87f51 to your computer and use it in GitHub Desktop.
normalize issue
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
response: '{"data":{"id":"35","type":"file-upload","attributes":{"file":"http://example.com/somefile"}, "relationships":{}}}',
data: null,
normal: null,
objPush: null,
objPushPayload: null,
actions: {
save: function() {
var response = this.get("response");
var data = JSON.parse(response);
this.set("data", JSON.stringify(data, null, 4));
console.log(data);
this.store.pushPayload(data);
var obj = this.store.peekRecord("file-upload", 35);
this.set("objPushPayload", obj);
// I believe the following should work as well:
//var normal = this.store.normalize("file-upload", data);
var normal = this.store.normalize("file-upload", data.data);
console.log(normal);
this.set("normal", JSON.stringify(normal, null, 4));
var objPush = this.store.push(normal);
this.set("objPush", objPush);
}
}
});
import DS from 'ember-data';
export default DS.Model.extend({
file: DS.attr("string")
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<h4>The inserted object via store.push(store.normalize)</h4>
{{objPush.file}}
<h4>The inserted object via store.pushPayload, store.peekRecord)</h4>
{{objPushPayload.file}}
<hr>
<button {{action "save"}}>Normalize</button>
<hr>
<h2>Response</h2>
<code>
{{response}}
</code>
<h2>Parsed</h2>
<pre>
{{data}}
</pre>
<h2>Normalized</h2>
<pre>
{{normal}}
</pre>
<br>
<br>
{
"version": "0.7.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.3/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.4.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.3/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment