Last active
August 29, 2015 13:56
-
-
Save chrism/8873579 to your computer and use it in GitHub Desktop.
Working munging from API into Ember Data format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Working munging from API into Ember Data format" /> | |
<meta charset="utf-8"> | |
<title>Ember Starter Kit</title> | |
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css"> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.2.1.js"></script> | |
<script src="http://builds.emberjs.com/ember-latest.js"></script> | |
<script src="http://builds.emberjs.com/ember-data-latest.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.3/jquery.mockjax.min.js"></script> | |
</head> | |
<body> | |
<script type="text/x-handlebars" data-template-name="application"> | |
<h1>belongs to relationship munging</h1> | |
{{outlet}} | |
</script> | |
<script type="text/x-handlebars" data-template-name="index"> | |
<h2>{{title}}</h2> | |
track state... {{track.state}} | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App = Ember.Application.create({}); | |
App.IndexRoute = Ember.Route.extend({ | |
model: function(){ | |
return this.store.find('video', '8a660002-03c6-4b8e-bd8b-4ce28fa0dacd'); | |
} | |
}); | |
App.Video = DS.Model.extend({ | |
title: DS.attr('string'), | |
track: DS.belongsTo('track', {embedded: true} ) | |
}); | |
App.Track = DS.Model.extend({ | |
state: DS.attr('string'), | |
videos: DS.hasMany('video') | |
}); | |
App.VideoSerializer = DS.RESTSerializer.extend({ | |
primaryKey: 'uuid', | |
extractSingle: function(store, type, payload, id, requestType) { | |
var tracks = []; | |
var track = payload.video.track; | |
var video = payload.video; | |
tracks.push(track); | |
video.track = payload.video.track.uuid; | |
console.log('track ... ' + JSON.stringify(tracks)); | |
console.log('video ... ' + JSON.stringify(video)); | |
payload = { video: video, track: tracks }; | |
return this._super(store, type, payload, id, requestType); | |
} | |
}); | |
App.TrackSerializer = DS.RESTSerializer.extend({ | |
primaryKey: 'uuid' | |
}); | |
$.mockjax({ | |
url: '/videos/8a660002-03c6-4b8e-bd8b-4ce28fa0dacd', | |
responseText: | |
{ | |
"video": { | |
"uuid": "8a660002-03c6-4b8e-bd8b-4ce28fa0dacd", | |
"state": "pending", | |
"theme": "basic", | |
"resolution": "nHD", | |
"title": "Test title", | |
"track": { | |
"uuid": "2", | |
"state": "complete", | |
"source": "upload" | |
} | |
} | |
} | |
}); | |
/* | |
$.mockjax({ | |
url: '/videos/8a660002-03c6-4b8e-bd8b-4ce28fa0dacd', | |
responseText: | |
{ | |
"video": { | |
"id": "8a660002-03c6-4b8e-bd8b-4ce28fa0dacd", | |
"state": "pending", | |
"theme": "basic", | |
"resolution": "nHD", | |
"title": "Test title", | |
"track": "2" | |
}, | |
"track": [{ | |
"id": "2", | |
"state": "complete", | |
"source": "upload" | |
}] | |
} | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment