Last active
August 29, 2015 14:19
-
-
Save barelyknown/0f7906cbd70906020a1b to your computer and use it in GitHub Desktop.
Updating Ember Relationship Example
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
| // When I have a route that provides a model through a relationship... | |
| // app/routes/organization/buyer/loads.js | |
| export default Ember.Route.extend(AuthenticatedRouteMixin, { | |
| model: function() { | |
| return this.modelFor('organization/buyer').get('loads'); | |
| } | |
| }); | |
| // And I receive new records via the server (via push or poll), | |
| // what is the right way to add those records in a way that | |
| // keeps the model in sync. | |
| // I see that if I call createRecord on the model, that "works". | |
| // $E is set to the instance of the organization/buyer/loads controller. | |
| $E.get('model').createRecord({id: '1234'}) | |
| // For testing, I can add a method to push a new record into the store | |
| // every few seconds using the format that server would return. | |
| // app/routes/organization/buyer/loads.js | |
| export default Ember.Route.extend(AuthenticatedRouteMixin, { | |
| model: function() { | |
| return this.modelFor('organization/buyer').get('loads'); | |
| }, | |
| startPushing: function() { | |
| this.push(); | |
| }.on('init'), | |
| push: function() { | |
| if (this.currentModel) { | |
| var id = (this.currentModel.length + 1).toString(); | |
| this.store.pushPayload({"data":{"id": id,"type":"loads","links":{"self":"http://localhost:5000/v1/loads/" + id,"buyer":{"self":"http://localhost:5000/v1/loads/" + id + "/links/buyer","related":"http://localhost:5000/v1/loads/" + id + "/buyer","linkage":{"type":"buyers","id":"1"}}}}}) | |
| } | |
| Ember.run.later(this, 'push', 3000); | |
| } | |
| }); | |
| // And that works fine! Is `pushPayload` the idiomatic way to do this? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If using
store.findof some sort you shouldn't need this. However, if you're using web sockets, polling, or straight jQuery, then you're right on track.See: http://guides.emberjs.com/v1.11.0/models/frequently-asked-questions (section titled "HOW DO I INFORM EMBER DATA ABOUT NEW RECORDS CREATED ON THE BACKEND?")