Created
April 25, 2014 20:58
-
-
Save tonywok/11303071 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <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"> | |
| </head> | |
| <body> | |
| <script type="text/x-handlebars"> | |
| {{outlet}} | |
| </script> | |
| <script type="text/x-handlebars" data-template-name="index"> | |
| {{render "naive" model}} | |
| </script> | |
| <script type="text/x-handlebars" data-template-name="naive"> | |
| {{model}} | |
| {{model.bars}} | |
| </script> | |
| <script type="text/x-data" data-model="foo" data-model-id="1"> | |
| { "foo": {"name": "red", "id": "1", "bars": [1,2]} } | |
| </script> | |
| <script type="text/x-data" data-model="bar" data-query='{"ids": [1, 2]}'> | |
| { | |
| "bars": [ | |
| {"id": 1, "name": "bar1"}, | |
| {"id": 2, "name": "bar2"} | |
| ] | |
| } | |
| </script> | |
| <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
| <script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script> | |
| <script src="http://builds.emberjs.com/tags/v1.0.0/ember.min.js"></script> | |
| <script src="http://builds.emberjs.com/canary/ember-data.min.js"></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
| // Boilerplate Ember App Stuff | |
| // | |
| App = Ember.Application.create(); | |
| App.Router.map(function() {}); | |
| App.IndexRoute = Ember.Route.extend({ | |
| model: function() { | |
| return this.get('store').find('foo', 1); | |
| } | |
| }); | |
| App.Foo = DS.Model.extend({ | |
| name: DS.attr('string'), | |
| bars: DS.hasMany("bar", {async: true}) | |
| }); | |
| App.Bar = DS.Model.extend({ | |
| name: DS.attr('string'), | |
| bar: DS.belongsTo("bar") | |
| }); | |
| // Stub ember data stuffs | |
| // | |
| App.ApplicationAdapter = DS.RESTAdapter.extend({ | |
| init: function() { | |
| var self = this; | |
| this._super(); | |
| this.PRELOADED_DATA = DATA = {}; | |
| Ember.$('script[type="text/x-data"]').each(function(i, el) { | |
| var $el = $(el); | |
| var model = $el.data('model'); | |
| var query = $el.data('query'); | |
| var id = $el.data('model-id'); | |
| var response = $el.text(); | |
| var url = self.buildURL(model, id); | |
| DATA[url] = { | |
| query: query ? query : undefined, | |
| response: response ? JSON.parse(response) : response | |
| }; | |
| }); | |
| }, | |
| ajax: function(url, type, hash) { | |
| var result = this.PRELOADED_DATA[url]; | |
| if (result && Em.compare(result.query, hash) === 0) { | |
| return Em.RSVP.Promise(function(resolve, reject) { | |
| Em.run(null, resolve, result.response); | |
| }); | |
| } else { | |
| return this._super(url, type, hash); | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment