Last active
August 29, 2015 13:57
-
-
Save pixelhandler/9480904 to your computer and use it in GitHub Desktop.
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
| html, body { margin: 20px; } | |
| h1 { font-size: 1.6em; padding-bottom: 10px; } | |
| h2 { font-size: 1.4em; } | |
| dl { padding: 15px; font-size: 1.4em; color: blue; } |
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="Ember Data (Canary) record w/ hasMany and belongsTo" /> | |
| <meta charset="utf-8"> | |
| <title>Ember Data (Canary) record w/ hasMany and belongsTo</title> | |
| <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css"> | |
| <!--link rel="stylesheet" href="jsbin.ziwuk.css"--> | |
| </head> | |
| <body> | |
| <script type="text/x-handlebars" id="application"> | |
| <h1>Ember Data (Canary) record w/ hasMany and belongsTo</h1> | |
| {{outlet}} | |
| </script> | |
| <script type="text/x-handlebars" id="index"> | |
| <h2>Club info:</h2> | |
| <dl> | |
| <dt>Team name:<dt> | |
| <dd>{{teamName}}</dd> | |
| <dt>Manager name:<dt> | |
| <dd>{{manager.managerName}}</dd> | |
| <dt>Players</dt> | |
| <dd>{{#each players}} | |
| {{playerName}}, {{club.teamName}}<br> | |
| {{/each}}</dd> | |
| </dl> | |
| <button {{action 'deleteClub' model}}>Delete</button> | |
| <button {{action 'logIntercepted'}}>Show Intercepted AJAX in console</button> | |
| </script> | |
| <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> | |
| <script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.3.0.js"></script> | |
| <script src="http://builds.emberjs.com/beta/ember.js"></script> | |
| <script src="http://builds.emberjs.com/beta/ember-data.js"></script> | |
| <script src="https://rawgithub.com/pixelhandler/ember-data-extensions/master/dist/embedded-adapter.js"></script> | |
| <script src="https://rawgithub.com/trek/FakeXMLHttpRequest/master/fake_xml_http_request.js"></script> | |
| <script src="https://rawgithub.com/trek/fakehr/master/fakehr.js"></script> | |
| <script type="text/javascript" charset="utf-8"> | |
| window.httpRespond = function (verb, url, body, status) { | |
| if(typeof body !== 'string'){ body = JSON.stringify(body); } | |
| var found = fakehr.match(verb.toUpperCase(), url) | |
| if (found){ | |
| Ember.run(function() { | |
| found.respond(status || 200, {'content-type': 'application/json'}, body); | |
| }); | |
| } else { | |
| throw new Ember.Error("No request intercepted for " + verb.toUpperCase() + " " + url + ". Intercepted requests were: " + fakehr.requests.map(function(r){ return r.method + " " + r.url}).join(", ")); | |
| } | |
| }; | |
| </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
| (function(host) { | |
| var playersCount = 3; | |
| var mockClub = { | |
| club: { | |
| id: 1, | |
| team_name: 'Dodgers', | |
| manager: { | |
| id: 1, | |
| club_id: 1, | |
| manager_name: 'Lasorda' | |
| }, | |
| players: [ | |
| { | |
| id: 1, | |
| club_id: 1, | |
| player_name: 'Scioscia' | |
| }, | |
| { | |
| id: 2, | |
| club_id: 1, | |
| player_name: 'Garvey' | |
| }, | |
| { | |
| id: 3, | |
| club_id: 1, | |
| player_name: 'Fernando' | |
| } | |
| ] | |
| } | |
| }; | |
| fakehr.start(); | |
| host.App = Ember.Application.create({ | |
| LOG_TRANSITIONS: true | |
| }); | |
| App.ApplicationAdapter = DS.EmbeddedAdapter; | |
| App.ApplicationSerializer = DS.EmbeddedSerializer.extend({ | |
| attrs: { | |
| players: { embedded: 'always' }, | |
| manager: { embedded: 'always' } | |
| } | |
| }); | |
| App.Club = DS.Model.extend({ | |
| players: DS.hasMany('player'), | |
| manager: DS.belongsTo('manager'), | |
| teamName: DS.attr('string') | |
| }); | |
| App.Player = DS.Model.extend({ | |
| playerName: DS.attr('string'), | |
| club: DS.belongsTo('club') | |
| }); | |
| App.Manager = DS.Model.extend({ | |
| club: DS.belongsTo('club'), | |
| managerName: DS.attr('string') | |
| }); | |
| App.IndexRoute = Ember.Route.extend({ | |
| model: function(){ | |
| var club = this.store.find('club', 1); | |
| Ember.run.later(function(){ | |
| httpRespond('get', '/clubs/1', JSON.stringify(mockClub), 200); | |
| }, 15); | |
| return club; | |
| }, | |
| actions: { | |
| deleteClub: function (model) { | |
| model.destroyRecord(); | |
| Ember.run.later(function(){ | |
| httpRespond('delete', '/clubs/1', '', 204); | |
| }, 15); | |
| }, | |
| logIntercepted: function () { | |
| fakehr.requests.forEach(function (req) { | |
| Em.A( Em.String.w('method status statusText readyState url responseText') ).forEach( | |
| function(prop) { | |
| console.log( prop, req[prop] ); | |
| if (prop.match(/^resp/) && req[prop]) { | |
| console.dir( JSON.parse(req[prop]) ); | |
| } | |
| } | |
| ); | |
| }); | |
| return false; | |
| } | |
| } | |
| }); | |
| }(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment